問題描述
我在這里有一個非常簡單的函數,我試圖在其中運行和測試,但是,它不輸出任何內容,也沒有任何錯誤.我已經多次檢查代碼,但沒有任何錯誤.
I have this very simple function right here in which I'm trying to run and test on, however, it doesn't output anything and it doesn't have any errors either. I've checked the code multiple times but it doesn't have any errors.
我打印了作業,結果如下:
I printed jobs and here's what I got:
[<Process(Process-12, stopped[1])>,
<Process(Process-13, stopped[1])>,
<Process(Process-14, stopped[1])>,
<Process(Process-15, stopped[1])>,
<Process(Process-16, stopped[1])>]
代碼如下:
import multiprocessing
def worker(num):
print "worker ", num
return
jobs = []
for i in range(5):
p = multiprocessing.Process(target = worker, args = (i,))
jobs.append(p)
p.start()
這是我期待的結果,但它沒有輸出任何東西:
Here's the result I'm expecting but it's not outputting anything:
Worker: 0
Worker: 1
Worker: 2
Worker: 3
Worker: 4
推薦答案
評論顯示 OP 使用 Windows 和 Spyder.由于 Spyder 重定向 stdout
而 Windows 不支持 forking,新的子進程不會打印到 Spyder 控制臺.這僅僅是因為新子進程的 stdout
是 Python 的 vanilla stdout,也可以在 sys.__stdout__
中找到.
The comments revealed that OP uses Windows as well as Spyder. Since Spyder redirects stdout
and Windows does not support forking, a new child process won't print into the Spyder console. This is simply due to the fact that stdout
of the new child process is Python's vanilla stdout, which can also be found in sys.__stdout__
.
有兩種選擇:
使用 logging 模塊.這將包括創建所有消息并將其記錄到一個或多個文件中.使用單個日志文件可能會導致輸出稍微亂碼的問題,因為進程會同時寫入文件.每個進程使用一個文件可以解決這個問題.
Using the logging module. This would encompass creating and logging all messages to one or several files. Using a single log-file may lead to the problem that the output is slightly garbled since the processes would write concurrently to the file. Using a single file per process could solve this.
不在子進程中使用print
,而只是將結果返回給主進程.通過使用 queue (或 multiprocessing.Manager().Queue()
因為分叉是不可能的)或更簡單地依靠 多處理池的 map
功能,請參見下面的示例.
Not using print
within the child processes, but simply returning the result to the main process. Either by using a queue (or multiprocessing.Manager().Queue()
since forking is not possible) or more simply by relying on the multiprocessing Pool's map
functionality, see example below.
帶有池的多處理示例:
import multiprocessing
def worker(num):
"""Returns the string of interest"""
return "worker %d" % num
def main():
pool = multiprocessing.Pool(4)
results = pool.map(worker, range(10))
pool.close()
pool.join()
for result in results:
# prints the result string in the main process
print(result)
if __name__ == '__main__':
# Better protect your main function when you use multiprocessing
main()
哪個打印(在主進程中)
which prints (in the main process)
worker 0
worker 1
worker 2
worker 3
worker 4
worker 5
worker 6
worker 7
worker 8
worker 9
<小時>
如果您迫不及待地等待 map
函數完成,您可以使用 imap_unordered
并稍微更改命令的順序立即打印結果:
If you are to impatient to wait for the map
function to finish, you can immediately print your results by using imap_unordered
and slightly changing the order of the commands:
def main():
pool = multiprocessing.Pool(4)
results = pool.imap_unordered(worker, range(10))
for result in results:
# prints the result string in the main process as soon as say are ready
# but results are now no longer in order!
print(result)
# The pool should join after printing all results
pool.close()
pool.join()
這篇關于Spyder中的簡單Python多處理函數不輸出結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!