适用于 Linux 和 Windows 的共享内存 IPC 解决方案

发布于 2025-01-14 04:47:21 字数 1275 浏览 3 评论 0原文

下面是 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 技术交流群。

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

发布评论

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

评论(2

瘫痪情歌 2025-01-21 04:47:24

就我个人而言,这对我来说效果很好

选项 1: http://www.inspirel.com/yami4/

用于通用计算的YAMI4套件是一个多语言和多平台包。

几种操作系统:

示例代码

Microsoft Windows、POSIX(Linux、Max OS X、FreeBSD...)、QNX(具有本机 IPC 消息传递)、FreeRTOS、ThreadX、TI-RTOS。编程语言:C++、Ada、Java、.NET、Python、Wolfram。

选项 2:ZeroMq https://zeromq .org/

Personally this has worked well for me

Option 1: http://www.inspirel.com/yami4/

The YAMI4 suite for general computing is a multi-language and multi-platform package.

Several Operating systems:

Sample code

Microsoft Windows, POSIX (Linux, Max OS X, FreeBSD, ...), QNX (with native IPC messaging), FreeRTOS, ThreadX, TI-RTOS. Programming languages: C++, Ada, Java, .NET, Python, Wolfram.

Option 2: ZeroMq https://zeromq.org/

一个人的夜不怕黑 2025-01-21 04:47:23

最简单的方法是使用版本>=3.8的python,它添加了内置的共享内存抽象,
它适用于 Windows 和 Linux
https://docs.python.org/3.10/library/multiprocessing.shared_memory。 html

代码看起来像这样:

Process #1:

  from multiprocessing import shared_memory
  # create=true to create a new shared memory instance, if it already exists with the same name, an exception is thrown
  shm_a = shared_memory.SharedMemory(name="example", create=True, size=10)
  shm_a.buf[:3] = bytearray([1, 2, 3])
  while True:
      do_smt()
  shm_a.close()

Process #2:

  from multiprocessing import shared_memory
  # create=false, use existing
  shm_a = shared_memory.SharedMemory(name="example", size=10)
  print(bytes(shm.buf[:3]))
  # [0x01, 0x02, 0x03]
  while True:
      do_smt()
  shm_a.close()

否则,我认为没有通用的好的解决方案,您将需要重新发明轮子:)

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:

  from multiprocessing import shared_memory
  # create=true to create a new shared memory instance, if it already exists with the same name, an exception is thrown
  shm_a = shared_memory.SharedMemory(name="example", create=True, size=10)
  shm_a.buf[:3] = bytearray([1, 2, 3])
  while True:
      do_smt()
  shm_a.close()

Process #2:

  from multiprocessing import shared_memory
  # create=false, use existing
  shm_a = shared_memory.SharedMemory(name="example", size=10)
  print(bytes(shm.buf[:3]))
  # [0x01, 0x02, 0x03]
  while True:
      do_smt()
  shm_a.close()

Otherwise, I think there are no common good solutions and you will need to reinvent the wheel :)

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