这是我的Python代码。
#!/usr/bin/env python
import multiprocessing
import time
def worker(q):
while True:
data = q.get()
print 'worker: got: %d' % data
if data == -1:
print 'worker: done'
break
def master():
q = multiprocessing.Queue()
p = multiprocessing.Process(target=worker, args=(q,))
p.start()
for i in range(5):
q.put(i)
print 'master: put: %d' % i
time.sleep(1)
q.put(-1)
p.join()
print 'master: done'
master()
print 'exiting ...'
这是我在Debian 9 GNU / Linux系统上运行此代码时的输出。
$ python q.py
master: put: 0
worker: got: 0
master: put: 1
worker: got: 1
master: put: 2
worker: got: 2
master: put: 3
worker: got: 3
master: put: 4
worker: got: 4
worker: got: -1
worker: done
master: done
exiting ...
我试图找到一些证据表明主进程和 工作进程正在通过传递队列中的数据进行通信 套接字,消息队列或共享内存。但我似乎找不到任何东西 支持它的证据。
$ ps -ef | grep python | grep -v grep; netstat -nopa | grep python; ipcs
lone 2914 9836 2 12:54 pts/1 00:00:00 python q.py
lone 2915 2914 0 12:54 pts/1 00:00:00 python q.py
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
------ Message Queues --------
key msqid owner perms used-bytes messages
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 163840 lone 600 393216 2 dest
0x00000000 262145 lone 600 393216 2 dest
0x00000000 360450 lone 600 393216 2 dest
0x00000000 142344195 lone 600 524288 2 dest
0x00000000 101941252 lone 600 4194304 2 dest
0x00000000 950277 lone 600 524288 2 dest
0x00000000 118194183 lone 600 696320 2 dest
0x00000000 118292488 lone 600 4153344 2 dest
0x00000000 117899273 lone 600 4153344 2 dest
0x00000000 118226954 lone 600 696320 2 dest
0x00000000 123535371 lone 600 86016 2 dest
0x00000000 123338764 lone 600 1728512 2 dest
0x00000000 123240461 lone 600 1798144 2 dest
0x00000000 123568144 lone 600 86016 2 dest
0x00000000 137330705 lone 600 32768 2 dest
0x00000000 137232402 lone 600 81920 2 dest
0x00000000 29098003 lone 600 4194304 2 dest
0x00000000 137265172 lone 600 81920 2 dest
0x00000000 137297941 lone 600 151552 2 dest
0x00000000 35258391 lone 600 393216 2 dest
0x00000000 35291160 lone 600 12288 2 dest
0x00000000 35323929 lone 600 12288 2 dest
0x00000000 35356698 lone 600 393216 2 dest
0x00000000 35520539 lone 600 12288 2 dest
0x00000000 35422236 lone 600 393216 2 dest
0x00000000 35455005 lone 600 12288 2 dest
------ Semaphore Arrays --------
key semid owner perms nsems
multiprocessing.Queue
主要流程到工作流程?multiprocessing.Queue
中的数据从主进程传输到工作进程的IPC机制的证据?答案 0 :(得分:0)
Python是开源的,你可以在这里清楚地看到实现:https://github.com/python/cpython/blob/master/Lib/multiprocessing/queues.py
它使用Pipe
来实现:https://github.com/python/cpython/blob/master/Lib/multiprocessing/connection.py
使用os.pipe()
,在此处实施:https://github.com/python/cpython/blob/master/Modules/posixmodule.c
使用pipe2()
,在此处记录:http://man7.org/linux/man-pages/man2/pipe.2.html