简体   繁体   English

如何在 C# 和 Python 进程之间共享字节数组的 memory

[英]How to share memory of a byte array between a C# and Python processes

I have a C# application that will continously allocate memory for data stored in byte arrays.我有一个 C# 应用程序,它将持续为存储在字节 arrays 中的数据分配 memory。 I have another process written in python that will read from these arrays once instantiated.我有另一个用 python 编写的进程,一旦实例化,它将从这些 arrays 中读取。 Both processes will be running on a ubuntu machine.这两个进程都将在 ubuntu 机器上运行。

The obvious solution seems to be to share memory between the processes by passing a pointer from the C# process to the python process.显而易见的解决方案似乎是通过将指针从 C# 进程传递给 python 进程来在进程之间共享 memory。 However, this has turned out to be difficult.然而,事实证明这很困难。

I've mainly looked at solutions proposed online.我主要看网上提出的解决方案。 Two notable ones are named pipes and mapped memory files.两个值得注意的是命名管道和映射的 memory 文件。 I read the following posts:我阅读了以下帖子:

Sharing memory between C and Python.在 C 和 Python 之间共享 memory。 Suggested to be done via named pipes: Share memory between C/C++ and Python建议通过命名管道完成: Share memory between C/C++ and Python

The C# application will neither read nor write from the array and the python script will only read from the array. C# 应用程序既不会从阵列中读取也不会从阵列中写入,并且 python 脚本只会从阵列中读取。 Therefore, this solution doesn't satisfy my efficiency requirements and seems to be a superfluous solution when the data is literally stored in memory.因此,这个解决方案不能满足我的效率要求,当数据字面上存储在 memory 中时,似乎是一个多余的解决方案。

When i looked at memory mapped files, it seemed as if though that we would allocate memory for these memory files to write the data to.当我查看 memory 映射文件时,似乎我们会为这些 memory 文件分配 memory 以将数据写入。 However, the data will already be allocated before the mapped file is used.但是,在使用映射文件之前,数据已经被分配。 Thus, it seems inefficient as well.因此,它似乎也效率低下。

The second post: https://docs.microsoft.com/en-us/dotnet/standard/io/memory-mapped-files?redirectedfrom=MSDN第二个帖子: https://docs.microsoft.com/en-us/dotnet/standard/io/memory-mapped-files?redirectedfrom=MSDN

The article says: "Starting with the .NET Framework 4, you can use managed code to access memory-mapped files in the same way that native Windows functions access memory-mapped files".文章说:“从 .NET 框架 4 开始,您可以使用托管代码访问内存映射文件,就像原生 Windows 函数访问内存映射文件一样”。 Would an ubuntu machine run into potential problems when reading these files in the same way that windows would? ubuntu 机器在以与 windows 相同的方式读取这些文件时会遇到潜在问题吗? And if not, could someone give either a simple example of using these mapped files between the program languages mentioned above as well as pass a reference to these mapped files between the processes, or give a reference to where someone has already done this?如果没有,有人可以举一个简单的例子,在上面提到的程序语言之间使用这些映射文件,并在进程之间传递对这些映射文件的引用,或者给出有人已经这样做的参考?

Or if someone knows how to directly pass a pointer to a byte array from C# to python, that would be even better if possible.或者,如果有人知道如何直接将指向字节数组的指针从 C# 传递到 python,如果可能的话,那就更好了。

Any help is greatly appreciated!任何帮助是极大的赞赏!

So after coming back to this post four months later, i did not find a solution that satisfied my efficiency needs.所以四个月后回到这个帖子后,我没有找到满足我效率需求的解决方案。

I had tried to find a way to get around having to write a large amount of data, already allocated in memory, to another process.我试图找到一种方法来避免将大量数据写入另一个进程,这些数据已经在 memory 中分配。 Meaning i would have needed to reallocate that same data taking up double the amount of memory and adding additional overhead even though the data would be read-safe.这意味着我需要重新分配相同的数据,占用两倍的 memory 并增加额外的开销,即使数据是读取安全的。 However, it seemed as though the proper way to solve this, in this case, for two processes running on the same machine would be to use named pipes as they are faster than ie sockets.然而,似乎解决这个问题的正确方法,在这种情况下,对于在同一台机器上运行的两个进程将使用命名管道,因为它们比 sockets 更快。 As Holger stated, this ended up being a question of interprocess-communication.正如 Holger 所说,这最终成为了一个进程间通信的问题。

I ended up writing the whole application in python which happened to be the better alternative in the end anyways, probably saving me a lot of headache.我最终在 python 中编写了整个应用程序,这最终恰好是更好的选择,可能让我省了很多麻烦。

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

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