[英]Python Multiprocessing queue.get() timeout when queue is not empty
[英]Python 3 - Multiprocessing - Queue.get() does not respond
我想進行蠻力攻擊,因此需要一定的速度...所以我上來使用多處理庫...但是,在我發現的每個教程中,都沒有任何效果....嗯..一個人似乎工作得很好,除了我每次調用get()函數時,閑置似乎都會進入睡眠狀態,並且它根本不響應。 我只是傻還是什么? 我只是復制粘貼了示例,所以它應該可以工作...。
import multiprocessing as mp
import random
import string
# Define an output queue
output = mp.Queue()
# define a example function
def rand_string(length, output):
""" Generates a random string of numbers, lower- and uppercase chars. """
rand_str = ''.join(random.choice(
string.ascii_lowercase
+ string.ascii_uppercase
+ string.digits)
for i in range(length))
output.put(rand_str)
# Setup a list of processes that we want to run
processes = [mp.Process(target=rand_string, args=(5, output)) for x in range(2)]
# Run processes
for p in processes:
p.start()
# Exit the completed processes
for p in processes:
p.join()
# Get process results from the output queue
results = [output.get() for p in processes]
print(results)
@dano撞上了頭! 如果沒有if __name__ == "__main__":
那么您就有一個“叉炸彈”。 也就是說,每個進程都在運行這些進程,依此類推。 您還將注意到,我已經移動了隊列的創建。
import multiprocessing as mp
import random
import string
# define a example function
def rand_string(length, output):
""" Generates a random string of numbers, lower- and uppercase chars. """
rand_str = ''.join(random.choice(
string.ascii_lowercase
+ string.ascii_uppercase
+ string.digits)
for i in range(length))
output.put(rand_str)
if __name__ == "__main__":
# Define an output queue
output = mp.Queue()
# Setup a list of processes that we want to run
processes = [mp.Process(target=rand_string, args=(5, output)) for x in range(2)]
# Run processes
for p in processes:
p.start()
# Exit the completed processes
for p in processes:
p.join()
# Get process results from the output queue
results = [output.get() for p in processes]
print(results)
發生的事情是, multiprocessing
進程將每個子進程作為一個模塊運行,因此__name__
在父__name__
中只是__main__
。 如果沒有,則每個子進程將(嘗試)啟動兩個以上的進程,每個進程將再啟動兩個,依此類推。 怪不得IDLE停下來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.