問題描述
我使用 Python 多處理模塊在 Monte Carlo 代碼中實現了一些簡單的并行性.我的代碼如下所示:
I've implemented some simple parallelism in a Monte Carlo code using the Python multiprocessing module. I have code that looks like:
但是,當我查看結果列表時,似乎蒙特卡羅迭代器甚至還沒有啟動.我知道他們有,因為我可以讓流程在蒙特卡羅步驟中打印出信息.所以我在做一些愚蠢的事情.我原以為 job.join() 會阻止結果列表的構建,直到一切都運行完畢,因此 mc.results 字段將被更新.
However, when I look at the results list, it looks like the monte carlo iterators haven't even started. I know that they have, because I can have the processes print out information during the monte carlo steps. So I'm doing something dumb. I had thought the job.join() would keep the results list from being constructed until everything had run, and thus the mc.results field would be updated.
我意識到我沒有告訴你我的 monte carlo 例程的詳細信息,希望這沒關系,我犯的錯誤在于我對多處理功能的解釋.提前感謝您提供的任何幫助.
I realize I haven't told you the details of my monte carlo routine, and hope that it doesn't matter, and that the mistake I'm making is in my interpretation of what multiprocessing does. Thanks in advance for any help you can offer.
推薦答案
MonteCarlo
對象已被腌制并發送到要運行的子進程 - .results
屬性在此過程中未填充,因為本地 mc
從未運行過.
The MonteCarlo
objects have been pickled and sent to child processes to be run - the .results
attribute in this process isn't populated because the local mc
has never been run.
如果你創建一個multiprocessing.Queue
,你可以將其傳遞給每個 MonteCarlo
作業,完成后它應該將結果放在那里.然后頂層可以等待隊列中的值.(在引擎蓋下,這將腌制和取消腌制結果對象.)
If you create a multiprocessing.Queue
, you can pass that into each MonteCarlo
job, and when it finishes it should put the result in there. Then the top-level can wait for values from the queue. (Under the hood this will pickle and unpickle the result object.)
這篇關于我可以從 multiprocessing.Process 中獲得返回值嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!