![](/img/trans.png)
[英]How to change my conda base environment from Python 3.7 to 3.9?
[英]Multiprocessing changed in python 3.9 from 3.7 on MacOSx, how to fix?
Python 3.9 多處理似乎在整個文件上運行了許多額外的循環,而 3.7 沒有這樣做。 該機器有 8 個核心,因此有 8 個循環。 我該如何解決。 這不是我所期望的——線程模塊是否也重新設計了,這個問題是否也發生在該模塊中。 這是代碼:
!/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' )
這是來自 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
這是來自 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
心理調試:你在 macOS 上。 在 3.8 中, multiprocessing
模塊從默認使用'fork'
啟動方法切換為'spawn'
啟動方法,因為高級 macOS 系統庫不是 fork-safe 並且可能導致崩潰。 'spawn'
方法模擬分叉的部分方法是重新導入__main__
模塊(但使用不同的__name__
,因此正確保護的模塊不會重做任何受保護的事情)。
如果您想擲骰子,請提前 go 並使用multiprocessing.set_start_method('fork')
明確選擇加入'fork'
啟動方法。 更恰當地說,修復您的代碼,使其遵循'spawn'
(和'forkserver'
)方法的multiprocessing
指南。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.