且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从python多重处理函数返回的多个输出

更新时间:2023-12-04 11:00:40

if __name__ == '__main__'放在错误的位置.一个快速的解决方案是仅保护对mp_factorizer的调用,如Janne Karila建议的那样:

if __name__ == '__main__' is in the wrong place. A quick fix would be to protect only the call to mp_factorizer like Janne Karila suggested:

if __name__ == '__main__':
    print mp_factorizer(list, 2, someobject)

但是,在Windows上,主文件将在执行时执行一次,而每个工作线程将执行一次,在这种情况下为2.因此,这将是主线程的总共3次执行,不包括代码的受保护部分.

However, on windows the main file will be executed once on execution + once for every worker thread, in this case 2. So this would be a total of 3 executions of the main thread, excluding the protected part of the code.

在同一主线程中进行其他计算时,这可能会导致问题,并且至少会不必要地降低性能.即使仅应多次执行worker函数,但在Windows中将执行不受if __name__ == '__main__'保护的所有内容.

This can cause problems as soon as there are other computations being made in the same main thread, and at the very least unnecessarily slow down performance. Even though only the worker function should be executed several times, in windows everything will be executed thats not protected by if __name__ == '__main__'.

因此解决方案将是通过仅在执行完所有代码后保护整个主进程 if __name__ == '__main__'.

So the solution would be to protect the whole main process by executing all code only after if __name__ == '__main__'.

但是,如果worker函数位于相同文件中,则需要将此if语句排除在外,因为否则无法多次调用它进行多重处理.

If the worker function is in the same file, however, it needs to be excluded from this if statement because otherwise it can not be called several times for multiprocessing.

伪代码主线程:

# Import stuff
if __name__ == '__main__':
    #execute whatever you want, it will only be executed 
    #as often as you intend it to
    #execute the function that starts multiprocessing, 
    #in this case mp_factorizer()
    #there is no worker function code here, it's in another file.

即使整个主进程都受到保护,只要它在另一个文件中,都可以启动worker函数.

Even though the whole main process is protected, the worker function can still be started, as long as it is in another file.

伪代码主线程,具有辅助函数:

Pseudocode main thread, with worker function:

# Import stuff
#If the worker code is in the main thread, exclude it from the if statement:
def worker():
    #worker code
if __name__ == '__main__':
    #execute whatever you want, it will only be executed 
    #as often as you intend it to
    #execute the function that starts multiprocessing, 
    #in this case mp_factorizer()
#All code outside of the if statement will be executed multiple times
#depending on the # of assigned worker threads.

有关可运行代码的详细说明,请参见在子流程中使用多处理 a>

For a longer explanation with runnable code, see using multiprocessing in a sub process