[英]what is the underlying process of python multiprocess queue?
有人詳細解釋 python 多處理隊列通信嗎? 將參數放入隊列時會發生什么? 我有一段代碼,這讓我很困惑。
import time
import numpy as np
from multiprocessing import Queue, Process
def task(queue_in, queue_out):
mutural_np = np.zeros((10, 2))
while True:
msg = queue_in.get()
res = []
i = 0
for i in range(msg):
newnp = np.ones((1, 2)) * (msg - i)
mutural_np[i:i+1] = newnp
res = mutural_np[:i]
print("===> put: ", res)
queue_out.put(res)
if __name__ == "__main__":
queue_in = Queue(10)
queue_out = Queue(1)
p1 = Process(target=task, args=(queue_in, queue_out))
p1.start()
for i in range(5):
queue_in.put(i + 1)
while True:
msg = queue_out.get()
time.sleep(0.5)
print("***> out: ", msg)
output 是:
===> put: []
===> put: [[2. 2.]]
===> put: [[3. 3.]
[2. 2.]]
***> out: []
===> put: [[4. 4.]
[3. 3.]
[2. 2.]]
***> out: [[3. 3.]]
===> put: [[5. 5.]
[4. 4.]
[3. 3.]
[2. 2.]]
***> out: [[4. 4.]
[3. 3.]]
***> out: [[5. 5.]
[4. 4.]
[3. 3.]]
***> out: [[5. 5.]
[4. 4.]
[3. 3.]
[2. 2.]]
為什么我有這種不一致?
我看到醫生說
" 當 object 被放入隊列時,object 被腌制,后台線程稍后將腌制數據刷新到底層 pipe。"
“pickle 模塊跟蹤它已經序列化的對象,以便以后對相同 object 的引用不會再次被序列化”
據我了解,當我將 object 放入隊列時,它是腌制且不可變的,但似乎腌制發生在它被沖洗掉之后。
我想我通過閱讀源代碼得到了答案。
當我將 object 放入進程隊列時,Python 啟動一個線程來序列化和發送數據。 因此,為了安全起見,請在將其放入隊列之前進行復制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.