目前,我使用subprocess
并行运行多个脚本,如下所示。 max_proc
似乎控制了我想要的处理器数量。以下是最小代码。
import os
import subprocess
def main():
args = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
max_proc = 4
processes = set()
for arg in args:
cmd = ['python', '-i', arg]
processes.add(subprocess.Popen(cmd))
if len(processes) >= max_proc:
os.wait()
processes.difference_update(
[p for p in processes if p.poll() is not None])
# Check if all the child processes were closed
for p in processes:
if p.poll() is None:
p.wait()
main()
在上面运行会是这样的:
# python -i a
# python -i b
# .. and so on
在这里,如果我想在参数中包含process-number
(可能从1到max_proc
),我该如何修改此代码?例如
# python -i a1
# python -i b2
# python -i c3
# python -i d4
# python -i e2
# python -i f1
# python -i h3
# python -i g4
# .. and so on
为此,我必须跟踪分配和完成的process-number
(因此可以重新分配)。有一个简单的方法吗?
*简而言之,我最多需要4个并行运行的进程。我还需要在参数中包含process-number
(1~4,或0~3,如果从零开始)。能否请你帮忙? *
答案 0 :(得分:1)
如果每个并行进程都有一个实际线程,那么该线程可以将其分配的数量视为其状态的一部分,并且只需将输入项从共享队列中拉出并按顺序处理它们。
作为示例(用您的真实程序替换echo
):
import os, subprocess, threading
program_to_call = 'echo' ## change this
def run(items, num):
try:
while True:
item = items.pop()
subprocess.call([program_to_call, str(num), str(item)])
except IndexError:
return ## "items" is empty
def main():
queue = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
max_proc = 4
threads = []
# start all four threads, passing each a different "num" and a reference to the queue
for num in range(max_proc):
thread = threading.Thread(target=run, args=(queue, num))
thread.start()
threads.append(thread)
# wait for all four threads to finish
for thread in threads:
thread.join()
if __name__ == '__main__':
main()