启动一个新进程并向其共享一个对象(共享内存)
Start a new process and share an object to it (shared memory)
进程 #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?
如果没有,我知道 sockets
(以及涉及网络的更普遍的消息传递技术),/dev/shm
Linux,这些 IPC techniques 使 2 个不同的进程进行通信,但是 仅共享内存中的对象 是否有更简单的解决方案?
我想,出于安全原因,进程应该以特殊选项启动,以授权其内存与其他进程共享。
经过进一步研究,似乎 mmap
非常适合这个(但请注意,它不会共享一个对象,而只是 bytes
。pickle
可用于序列化).
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.
因此,这是一个非常简单的shared-memory进程间通信,无需使用套接字:
服务器:
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:
Avoiding the use of the tag parameter will assist in keeping your code portable between Unix and Windows.
视频数据的另一个例子:https://github.com/off99555/python-mmap-ipc
进程 #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?
如果没有,我知道 sockets
(以及涉及网络的更普遍的消息传递技术),/dev/shm
Linux,这些 IPC techniques 使 2 个不同的进程进行通信,但是 仅共享内存中的对象 是否有更简单的解决方案?
我想,出于安全原因,进程应该以特殊选项启动,以授权其内存与其他进程共享。
经过进一步研究,似乎 mmap
非常适合这个(但请注意,它不会共享一个对象,而只是 bytes
。pickle
可用于序列化).
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.
因此,这是一个非常简单的shared-memory进程间通信,无需使用套接字:
服务器:
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:
Avoiding the use of the tag parameter will assist in keeping your code portable between Unix and Windows.
视频数据的另一个例子:https://github.com/off99555/python-mmap-ipc