简体   繁体   English

如何同时运行 10 个 python 程序?

[英]How to run 10 python programs simultaneously?

I have a_1.py ~ a_10.py我有a_1.py ~ a_10.py

I want to run 10 python programs in parallel.我想并行运行 10 个 python 程序。

I tried:我试过:

from multiprocessing import Process
import os

def info(title):
    I want to execute python program

def f(name):
    for i in range(1, 11):
        subprocess.Popen(['python3', f'a_{i}.py'])


if __name__ == '__main__':
    info('main line')
    p = Process(target=f)
    p.start()
    p.join()

but it doesn't work但它不起作用

How do I solve this?我该如何解决这个问题?

I would suggest using the subprocess module instead of multiprocessing :我建议使用subprocess模块而不是multiprocessing

import os
import subprocess
import sys


MAX_SUB_PROCESSES = 10

def info(title):
    print(title, flush=True)

if __name__ == '__main__':
    info('main line')

    # Create a list of subprocesses.
    processes = []
    for i in range(1, MAX_SUB_PROCESSES+1):
        pgm_path = f'a_{i}.py'  # Path to Python program.
        command = f'"{sys.executable}" "{pgm_path}" "{os.path.basename(pgm_path)}"'
        process = subprocess.Popen(command, bufsize=0)
        processes.append(process)

    # Wait for all of them to finish.
    for process in processes:
        process.wait()

    print('Done')

If you just need to call 10 external py scripts ( a_1.py ~ a_10.py ) as a separate processes - use subprocess.Popen class:如果你只需要调用10个外部py脚本( a_1.pya_10.py )作为一个单独的进程-使用subprocess.Popen类:

import subprocess, sys

for i in range(1, 11):
    subprocess.Popen(['python3', f'a_{i}.py'])

# sys.exit()   # optional

It's worth to look at a rich subprocess.Popen signature (you may find some useful params/options)值得看看丰富的subprocess.Popen签名(你可能会发现一些有用的参数/选项)

You can use a multiprocessing pool to run them concurrently.您可以使用多处理池来同时运行它们。

import multiprocessing as mp

def worker(module_name):
    """ Executes a module externally with python """
    __import__(module_name)
    return

if __name__ == "__main__":
    max_processes = 5
    module_names = [f"a_{i}" for i in range(1, 11)]
    print(module_names)
    with mp.Pool(max_processes) as pool:
        pool.map(worker, module_names)

The max_processes variable is the maximum number of workers to have working at any given time. max_processes变量是在任何给定时间工作的最大工人数。 In other words, its the number of processes spawned by your program.换句话说,它是您的程序产生的进程数。 The pool.map(worker, module_names) uses the available processes and calls worker on each item in your module_names list. pool.map(worker, module_names)使用可用进程并在 module_names 列表中的每个项目上调用 worker。 We don't include the .py because we're running the module by importing it.我们不包含 .py,因为我们通过导入来运行模块。

Note: This might not work if the code you want to run in your modules is contained inside if __name__ == "__main__" blocks.注意:如果要在模块中运行的代码包含在if __name__ == "__main__"块中,这可能不起作用。 If that is the case, then my recommendation would be to move all the code in the if __name__ == "__main__" blocks of the a_{} modules into a main function.如果是这种情况,那么我的建议是将a_{}模块的if __name__ == "__main__"块中的所有代码移动到main函数中。 Additionally, you would have to change the worker to something like:此外,您必须将工作人员更改为以下内容:

def worker(module_name):
    module = __import__(module_name) # Kind of like 'import module_name as module'
    module.main()
    return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM