启动一个新进程并为其共享一个对象(共享内存)

发布于 2025-01-13 00:12:21 字数 687 浏览 3 评论 0原文

进程 #1 是否可以使用 Popen 启动进程 #2,并向其传递对字典 D 的引用,以便进程 #2 可以读取其内容?

# process1.py
import subprocess, time
D = {'foo': 'bar'}
subprocess.Popen(['python3', 'process2.py', str(id(D))])
time.sleep(3600)

# process2.py
import sys
ref = sys.argv[1]  # can we turn this address into an (at least read-only) object?
                   # and read foo/bar?

如果没有,我知道 socket (以及涉及网络的更一般的消息传递技术)、Linux 上的 /dev/shm 以及这些 IPC 技术 使两个不同的进程进行通信,但是是否有更简单的解决方案,仅共享内存中的对象< /强>?
我想,出于安全原因,进程应该使用特殊选项来启动,以授权其内存与其他进程共享。

Can a process #1 start a process #2 with Popen, and pass to it a reference to a dict D, so that process #2 can read its content?

# process1.py
import subprocess, time
D = {'foo': 'bar'}
subprocess.Popen(['python3', 'process2.py', str(id(D))])
time.sleep(3600)

# process2.py
import sys
ref = sys.argv[1]  # can we turn this address into an (at least read-only) object?
                   # and read foo/bar?

If not, I know sockets (and more generally messaging techniques involving networking), /dev/shm on Linux, and these IPC techniques to make 2 different processes communicate, but are there even simpler solutions by just sharing an in-memory object?
I guess, for security reasons, the processes should be started with a special option to authorize its memory to be shared with other processes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必了 2025-01-20 00:12:21

经过进一步研究,似乎 mmap 非常适合这种情况(但请注意,它不会共享对象,而只是共享字节。不过,pickle 可用于序列化)。

要映射匿名内存,-1 应作为文件号和长度一起传递。

tagname(如果指定且未指定 None)是一个字符串,为映射提供标记名称。 Windows 允许您对同一文件有许多不同的映射。如果指定现有标签的名称,则打开该标签,否则将创建具有该名称的新标签。

因此,这是一个非常简单的共享内存进程间通信,无需使用套接字:

服务器:

import mmap, time
mm = mmap.mmap(-1, 20, tagname="foo")
while True:
    mm.seek(0)
    mm.write(str(time.time()).encode())
    mm.flush()
    time.sleep(1)

客户端:

import mmap, time
mm = mmap.mmap(-1, 20, tagname="foo")
while True:
    mm.seek(0)
    buf = mm.read(128)
    print(buf)
    time.sleep(1)

注意:这仅适用于 Windows:

避免使用标签参数将有助于保持代码在 Unix 和 Windows 之间可移植。

视频数据的另一个示例: https://github.com/off99555/python-mmap-ipc< /a>

After further research, it seems mmap is perfect for this (but note that it will not share an object but just bytes. pickle can be used for the serialization though).

To map anonymous memory, -1 should be passed as the fileno along with the length.

tagname, if specified and not None, is a string giving a tag name for the mapping. Windows allows you to have many different mappings against the same file. If you specify the name of an existing tag, that tag is opened, otherwise a new tag of this name is created.

Thus, this is a very simple shared-memory inter process communication without using a socket:

Server:

import mmap, time
mm = mmap.mmap(-1, 20, tagname="foo")
while True:
    mm.seek(0)
    mm.write(str(time.time()).encode())
    mm.flush()
    time.sleep(1)

Client:

import mmap, time
mm = mmap.mmap(-1, 20, tagname="foo")
while True:
    mm.seek(0)
    buf = mm.read(128)
    print(buf)
    time.sleep(1)

Note: this is only for Windows:

Avoiding the use of the tag parameter will assist in keeping your code portable between Unix and Windows.

Another example with video data: https://github.com/off99555/python-mmap-ipc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文