适用于 Linux 和 Windows 的共享内存 IPC 解决方案
下面是 Windows 上具有共享内存的 IPC 的简单而完美的解决方案,无需使用网络/套接字(具有 Windows 上烦人的限制)。 唯一的问题是它在 Linux 上不可移植:
避免使用标签参数将有助于保持代码在 Unix 和 Windows 之间可移植。
问题:Python 中是否有一种内置的简单方法,无需条件分支“如果平台是 Windows,如果平台是 Linux”即可拥有共享内存 mmap
?
类似的东西
mm = sharedmemory(size=2_000_000_000, name="id1234") # 2 GB, id1234 is a global
# id available for all processes
mm.seek(1_000_000)
mm.write(b"hello")
在 Windows 上内部默认为 mmap.mmap(..., tagname="id1234")
并在 Linux 上使用 /dev/shm
(甚至可能我不知道更好的解决方案?),以及Mac 上可能有其他东西,但不必为每个不同的操作系统手动处理这个问题。
仅适用于 Windows 的解决方案:
#server
import mmap, time
mm = mmap.mmap(-1, 1_000_000_000, tagname="foo")
while True:
mm.seek(500_000_000)
mm.write(str(time.time()).encode())
mm.flush()
time.sleep(1)
# client
import mmap, time
mm = mmap.mmap(-1, 1_000_000_000, tagname="foo")
while True:
mm.seek(500_000_000)
print(mm.read(128))
time.sleep(1)
Below is a simple and perfect solution on Windows for IPC with shared memory, without having to use networking / sockets (that have annoying limits on Windows).
The only problem is that it's not portable on Linux:
Avoiding the use of the tag parameter will assist in keeping your code portable between Unix and Windows.
Question: is there a simple way built-in in Python, without having a conditional branch "if platform is Windows, if platform is Linux" to have a shared-memory mmap
?
Something like
mm = sharedmemory(size=2_000_000_000, name="id1234") # 2 GB, id1234 is a global
# id available for all processes
mm.seek(1_000_000)
mm.write(b"hello")
that would internally default to mmap.mmap(..., tagname="id1234")
on Windows and use /dev/shm
on Linux (or maybe even a better solution that I don't know?), and probably something else on Mac, but without having to handle this manually for each different OS.
Working Windows-only solution:
#server
import mmap, time
mm = mmap.mmap(-1, 1_000_000_000, tagname="foo")
while True:
mm.seek(500_000_000)
mm.write(str(time.time()).encode())
mm.flush()
time.sleep(1)
# client
import mmap, time
mm = mmap.mmap(-1, 1_000_000_000, tagname="foo")
while True:
mm.seek(500_000_000)
print(mm.read(128))
time.sleep(1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就我个人而言,这对我来说效果很好
选项 1: http://www.inspirel.com/yami4/
几种操作系统:
示例代码
选项 2:ZeroMq https://zeromq .org/
Personally this has worked well for me
Option 1: http://www.inspirel.com/yami4/
Several Operating systems:
Sample code
Option 2: ZeroMq https://zeromq.org/
最简单的方法是使用版本>=3.8的python,它添加了内置的共享内存抽象,
它适用于 Windows 和 Linux
https://docs.python.org/3.10/library/multiprocessing.shared_memory。 html
代码看起来像这样:
Process #1:
Process #2:
否则,我认为没有通用的好的解决方案,您将需要重新发明轮子:)
The easiest way is to use python with version >=3.8, it has added a built-in abstraction for shared memory,
it works on both windows and linux
https://docs.python.org/3.10/library/multiprocessing.shared_memory.html
The code will look something like this:
Process #1:
Process #2:
Otherwise, I think there are no common good solutions and you will need to reinvent the wheel :)