簡體   English   中英

多處理中的無限循環 python

[英]Infinite loops in multiprocessing python

如何讓 notworking() 在 multiprossesing 中工作。 我的控制台只記錄Whyisthis()。 我是 multiprosessing 的新手,我只是不明白,所以我希望有人能給出一個簡單的解決方案。

from multiprocessing import Process
def whyisthis():
    while True:
        print(f'Why is this')
        time.sleep(5)


def notworking():
    while True:
        print(f'not working')


p1 = Process(target=whyisthis(), daemon=False, 
name='Why is this')
p2 = Process(target=notworking(), daemon=False, 
name='Not working')

if __name__ == '__main__':
    p1.start()
    p2.start()
    p1.join()
    p2.join()

您的代碼的問題在於:

p1 = Process(target=whyisthis(), daemon=False, name="Why is this")

目標應該是一個函數,但在您的代碼中,您執行function 此處帶括號。 這就像正常的 function 調用一樣,這就是為什么您的程序只是停留在該行(或更准確地說 - 執行為什么永遠不會結束)。

正確的方法應該是

p1 = Process(target=whyisthis, daemon=False, name="Why is this")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM