简体   繁体   English

多处理在 python 3.9 中从 MacOSx 上的 3.7 更改,如何修复?

[英]Multiprocessing changed in python 3.9 from 3.7 on MacOSx, how to fix?

Python 3.9 multiprocessing seems to run many extra loops over the whole file, while 3.7 didnt do this. Python 3.9 多处理似乎在整个文件上运行了许多额外的循环,而 3.7 没有这样做。 The machine has 8 cores thus 8 loops.该机器有 8 个核心,因此有 8 个循环。 How do I fix this.我该如何解决。 This is not what I am expecting -- was the threading module also reworked, does this problem happen in that module also.这不是我所期望的——线程模块是否也重新设计了,这个问题是否也发生在该模块中。 This is the code:这是代码:

 !/usr/bin/python
from   time            import strftime
import multiprocessing as mp

print (strftime("%H:%M:%S") + ' Run line5 ' )

def pullLast():  
    print(12345678998765432345678)
    return 1

def mprocULwork(uplist): # build a queue with tuple
    ULsyms = [e for e in uplist if e is not None] 
    p_size          = mp.cpu_count()
    pool            = mp.Pool(processes=p_size, maxtasksperchild=400,)
    pool_outputs    = pool.map(ULProcess, ULsyms)
    pool.close()    # no more tasks
    pool.join()     # wrap up current tasks
    del pool
    print (strftime("%H:%M:%S"), "Finished ULwork")

def ULProcess(symbol):
    pSet    = 0
    print(pSet,symbol)
    
if __name__ == '__main__': 
    pSet    = 1
    symlist = ["G","Y","S" ]
    ullist  = symlist

    global lastPriceDF
    lastPriceDF  = pullLast()

    mprocULwork(ullist)                ####### <<<<<<<<<<<<<<<<<<main entry
    print (strftime("%H:%M:%S"), "post")
   
print (strftime("%H:%M:%S"),  'Exiting....line last' )

This the output from python 3.7:这是来自 python 3.7 的 output:

10:08:58 Run line5 
12345678998765432345678
0 G
0 Y
0 S
10:08:58 Finished ULwork
10:08:58 post
10:08:58 Exiting....line last

This is the output from 3.9:这是来自 3.9 的 output:

10:20:44 Run line5 
12345678998765432345678
10:20:44 Run line5 
10:20:44 Exiting....line last
0 G
0 Y
0 S
10:20:44 Run line5 
10:20:44 Exiting....line last
10:20:44 Run line5 
10:20:44 Exiting....line last
10:20:44 Run line5 
10:20:44 Exiting....line last
10:20:44 Run line5 
10:20:44 Exiting....line last
10:20:44 Run line5 
10:20:44 Exiting....line last
10:20:44 Run line5 
10:20:44 Exiting....line last
10:20:44 Run line5 
10:20:44 Exiting....line last
10:20:44 Finished ULwork
10:20:44 post
10:20:44 Exiting....line last

Psychic debugging: You're on macOS.心理调试:你在 macOS 上。 In 3.8, the multiprocessing module switched from using the 'fork' start method by default to the 'spawn' start method , because the high-level macOS system libraries are not fork-safe and can cause crashes .在 3.8 中, multiprocessing模块从默认使用'fork'启动方法切换为'spawn'启动方法,因为高级 macOS 系统库不是 fork-safe 并且可能导致崩溃 Part of how the 'spawn' method simulates forking is by reimporting the __main__ module (but with a different __name__ so a properly guarded module won't redo anything that's guarded). 'spawn'方法模拟分叉的部分方法是重新导入__main__模块(但使用不同的__name__ ,因此正确保护的模块不会重做任何受保护的事情)。

If you want to roll the dice, go ahead and explicitly opt-in to the 'fork' start method with multiprocessing.set_start_method('fork') .如果您想掷骰子,请提前 go 并使用multiprocessing.set_start_method('fork')明确选择加入'fork'启动方法。 More properly, fix your code to make it follow the multiprocessing guidelines for the 'spawn' (and 'forkserver' ) method .更恰当地说,修复您的代码,使其遵循'spawn' (和'forkserver' )方法的multiprocessing指南

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

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