简体   繁体   English

Python 使用 mmap 和空文件共享 Memory

[英]Python Shared Memory using mmap and empty files

I'm trying to make a fast library for interprocess communication between any combination of Python and C/C++ processes.我正在尝试为 Python 和 C/C++ 进程的任意组合之间的进程间通信创建一个快速库。 (ie Python <-> Python, Python <-> C++, or C++ <-> Python)

In the hopes of having the fastest implementation, I'm trying to utilize shared memory using mmap.为了实现最快的实现,我尝试使用 mmap 来利用共享的 memory。 The plan is for two processes to share memory by "mmap-ing" the same file and read from and write to this shared memory to communicate.计划是让两个进程通过“mmap-ing”同一个文件来共享 memory,并读写这个共享的 memory 以进行通信。

I want to avoid any actual writes to a real file, and instead simply want to use a filename as a handle for the two processes to connect.我想避免对真实文件进行任何实际写入,而只是想使用文件名作为两个进程连接的句柄。 However, I get hung up on the following call to mmap :但是,我挂断了对mmap的以下调用:

self.memory = mmap.mmap(fileno, self.maxlen)

where I get the following error:我收到以下错误:

FileNotFoundError: [Errno 2] No such file or directory: 'shared_memory_file'

or if I make an empty file:或者如果我制作一个空文件:

ValueError: mmap length is greater than file size

Do I need to simply make an empty file filled with nulls in order to be able to use shared memory like this?为了能够像这样使用共享的 memory,我是否需要简单地制作一个充满空值的空文件?

How can I use mmap for shared memory in Python between unrelated processes (not parent<->child communication) in a way which C++ can also play along?如何以 C++ 也可以播放的方式在不相关进程(不是父<->子通信)之间的 Python 中使用 mmap 共享 memory? (not using multiprocessing.shared_memory) (不使用 multiprocessing.shared_memory)

To answer the questions directly as best I can:为了尽可能直接回答问题,我可以:

  • The file needs to be sized appropriately before it can be mapped.在映射文件之前,需要适当调整文件大小。 If you need more space, there are different ways to do it... but most portable is likely unmap the file, resize the file on disk, and then remap the file.如果您需要更多空间,有不同的方法可以做到......但大多数便携式可能会取消映射文件,调整磁盘上的文件大小,然后重新映射文件。 See: How to portably extend a file accessed using mmap()请参阅: 如何可移植地扩展使用 mmap() 访问的文件

  • You might be able to mmap with MAP_ANONYMOUS|MAP_SHARED, then fork, then run with the same shared memory in both processes.您可以使用 MAP_ANONYMOUS|MAP_SHARED 进行 mmap,然后 fork,然后在两个进程中使用相同的共享 memory 运行。 See: Sharing memory between processes through the use of mmap()请参阅: 通过使用 mmap() 在进程之间共享 memory

  • Alternatively, you could create a ramdisk, create a file there of a specific size, and then mmap into both processes.或者,您可以创建一个 ramdisk,在其中创建一个特定大小的文件,然后将其映射到两个进程中。

  • Keep in mind that you'll need to deal with synchronization between the two processes - different platforms might have different approaches to this, but they traditionally involve using a semaphore of some kind (eg on Linux: https://man7.org/linux/man-pages/man7/sem_overview.7.html ).请记住,您需要处理两个进程之间的同步 - 不同的平台可能有不同的方法,但它们传统上涉及使用某种信号量(例如在 Linux 上: https://man7.org/linux /man-pages/man7/sem_overview.7.html )。

All that being said, traditional shared memory will probably do better than mmap for this use-case.话虽如此,传统的共享 memory 在这个用例中可能会比 mmap 做得更好。 In general, OS-level IPC mechanisms are likely to do better out of the box than hand-rolled solutions - there's a lot of tuning that goes into something to make it perform well, and mmap isn't always an automatic win.一般来说,操作系统级别的 IPC 机制开箱即用可能比手动解决方案做得更好——为了使其表现良好,需要进行大量调整,并且 mmap 并不总是自动获胜。

Good luck with the project!祝项目顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM