python多进程队列的底层进程是什么?
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)
输出为:
===> 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.]]
为什么我有这种不一致?
我看到文档说
" When an object is put on a queue, the object is pickled and a background thread later flushes the pickled data to an underlying pipe. "
"The pickle module keeps track of the objects it has already serialized so that later references to the same object won’t be serialized again"
据我了解,当我将对象放入队列时,该对象已被 pickle 且不可变,但似乎 pickle 发生在它被清除后。
我想我通过阅读源代码找到了答案。
当我将对象放入进程队列时,Python 启动一个线程来序列化并发送数据。所以,为了安全起见,在放入队列之前复制一份。
有人详细解释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)
输出为:
===> 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.]]
为什么我有这种不一致?
我看到文档说
" When an object is put on a queue, the object is pickled and a background thread later flushes the pickled data to an underlying pipe. "
"The pickle module keeps track of the objects it has already serialized so that later references to the same object won’t be serialized again"
据我了解,当我将对象放入队列时,该对象已被 pickle 且不可变,但似乎 pickle 发生在它被清除后。
我想我通过阅读源代码找到了答案。
当我将对象放入进程队列时,Python 启动一个线程来序列化并发送数据。所以,为了安全起见,在放入队列之前复制一份。