简体   繁体   English

主模块以外的模块中的Python多处理启动过程

[英]Python Multiprocessing Start Process in Module Other Than Main

I have three modules, worker , master , and MainTests . 我有三个模块, workermasterMainTests I'm running the MainTests module as the main script. 我正在运行MainTests模块作为主要脚本。 In MainTests , I call master.run() , inside of which I need to spawn multiple worker processes. MainTests ,我调用master.run() ,在其中需要生成多个worker进程。 Is this possible? 这可能吗? In all the python multiprocessing tutorials I have come across, processes are started in the main module. 在我遇到的所有python multiprocessing教程中,过程都是在主模块中启动的。 If this is possible, could someone provide an example as to what this might look like? 如果可能的话,有人可以提供一个例子说明一下吗?

This is what I have attempted so far: 到目前为止,这是我尝试过的:

Worker.py 工人

import time

class Worker(object):
    def __init__(self):
        super(Worker, self).__init__()
    def run(self):
        time.sleep(5)
        print("worker done with run")
        return

Master.py: Master.py:

import multiprocessing

class Master(object):
    def __init__(self, workers_array):
        super(Master, self).__init__()
        self.workers_array = workers_array
    def run(self):
        process_arr = [multiprocessing.Process(worker.run()) for worker in self.workers_array]
        [worker_process.start() for worker_process in process_arr]

MainTests.py MainTests.py

from Worker import *
from Master import *

workers_array = [Worker() for i in range(5)]
master = Master(workers_array)
master.run()

Two issues arise: 出现两个问题:

  1. Workers seem to be running sequentially, one by one executing run() rather than running in parallel. 工作进程似乎是依次运行,一个接一个地运行run()而不是并行运行。
  2. Workers seem to keep repeating runs. 工人似乎一直在重复跑步。 I would expect that after the workers complete their runs, the program ends, but it keeps going. 我希望在工作人员完成运行之后,该程序会结束,但仍会继续。

Thanks in advance for any help. 在此先感谢您的帮助。

I'm not sure if you're still locking for an answer but you just have to put the "entry point" of your main program in this if statement: 我不确定您是否仍在锁定答案,但是您只需要在以下if语句中输入主程序的“入口点”即可:

if __name__ == "__main__":
    main()

This way you can start a process in an imported module. 这样,您可以在导入的模块中启动流程。 For further information check out this bit of the docs and obviously everything else in there :) 有关更多信息,请查看文档的这一部分以及其中的所有其他内容:)

In your example it would be: 在您的示例中,它将是:

from Worker import *
from Master import *

if __name__ == __main__:
    workers_array = [Worker() for i in range(5)]
    master = Master(workers_array)
    master.run()

This has worked for me. 这对我有用。 Hope I could help. 希望我能帮上忙。

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

相关问题 Python 多处理进程是否可以从 __main__ 以外的其他地方启动? - Can Python multiprocessing process be launched from OTHER than __main__? Python 多处理:主进程的名称 - Python multiprocessing: name of the main process 对主进程的多处理回调 - Python - Multiprocessing Callbacks to Main Process - Python 在多处理模块中为每个进程重新加载Python模块 - Reloading a Python module per process in the multiprocessing module 在Windows上启动新进程时,为什么Python的多处理模块会导入__main__? - Why does Python's multiprocessing module import __main__ when starting a new process on Windows? Python和多处理......如何在主进程中调用函数? - Python and multiprocessing… how to call a function in the main process? 使用Pool进行Python多处理-主过程永远需要 - Python multiprocessing with Pool - the main process takes forever Python 主模块外的多处理:计算缓慢 - Python multiprocessing outside main module: slow computations Python,多处理模块,Process类,启动方法失败? 启动无限的口译员:| - Python, multiprocessing module, Process class, start method fail? Starts up an infinity of interpreters :| python:在wxbutton上单击以启动一个多处理过程 - python: start a Multiprocessing process on wxbutton click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM