【问题标题】:What IPC mechanism is used to share the data in multiprocessing.Queue between two Python processes?使用什么 IPC 机制在两个 Python 进程之间共享 multiprocessing.Queue 中的数据?
【发布时间】:2018-01-13 03:26:30
【问题描述】:

这是我的 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 机制的证据?

【问题讨论】:

  • 这是一个有趣的问题。 multiprocessing 设计为跨平台。我不确定是否使用了 POSIX IPC。
  • @cᴏʟᴅsᴘᴇᴇᴅ 查看 John Zwinck 的答案。

标签: python linux python-2.7 multiprocessing ipc


【解决方案1】:

【讨论】:

    猜你喜欢
    • 2021-07-28
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 2013-03-23
    相关资源
    最近更新 更多