简体   繁体   English

如何在python和节点进程之间共享mmap

[英]How to share mmap between python and node processes

I'm trying to share memory between a python process and a nodejs process started from the python process using an anonymous mmap.我正在尝试使用匿名 mmap 在 python 进程和从 python 进程启动的 nodejs 进程之间共享内存。 Essentially, the python process begins, initializes the mmap and starts a subprocess using either call or Popen to launch a child that runs some node code.本质上,python 进程开始,初始化 mmap 并使用 call 或 Popen 启动子进程以启动运行某些节点代码的子进程。 This nodejs code uses mmap to try to access the same area in memory.此 nodejs 代码使用 mmap 尝试访问内存中的相同区域。 However I get two different mappings and no data is shared between them.但是我得到了两个不同的映射,并且它们之间没有共享数据。 Why is this?为什么是这样?

import mmap, math, os
from subprocess import call

mm = mmap.mmap( -1, 1024,
                flags=mmap.MAP_SHARED | mmap.MAP_ANONYMOUS,
                prot= mmap.PROT_READ | mmap.PROT_WRITE )


mm.seek(0)
mm.write('hello world!\n'.encode('utf-8'))



call([
    'node', '-e',
    """
const mmap = require('mmap.js');

const fileBuf = mmap.alloc(
    1024,
    mmap.PROT_READ | mmap.PROT_WRITE,
    mmap.MAP_SHARED| mmap.MAP_ANONYMOUS,
    -1,
    0
)

console.log(fileBuf.toString('utf-8'));

    """ 
])

The mmap.js that I am using is a NAPI of the original mmap c function.我使用的 mmap.js 是原始 mmap c 函数的 NAPI。 This is the github for this library.这是这个库的github。

EDIT:编辑:

Thanks to 'that other guy' for his answer.感谢“那个其他人”的回答。 It was the correct one.这是正确的。 Here's some sample code that works out of the box!: test_mmap.py这是一些开箱即用的示例代码!:test_mmap.py

import os, ctypes, posix_ipc, sys, mmap
from subprocess import call
SHARED_MEMORY_NAME = "/shared_memory"



memory = posix_ipc.SharedMemory(SHARED_MEMORY_NAME, posix_ipc.O_CREX,
                                size=1024)

mapFile = mmap.mmap(memory.fd, memory.size)
memory.close_fd()


mapFile.seek(0)
mapFile.write("Hello world!\n".encode('utf-8'))
mapFile.seek(0)

print("FROM PYTHON MAIN PROCESS: ", mapFile.readline().decode('utf-8'))
mapFile.seek(0)



call([
    "node", "./test_mmap.js", SHARED_MEMORY_NAME
])





mapFile.close()


posix_ipc.unlink_shared_memory(SHARED_MEMORY_NAME)

test_mmap.js test_mmap.js

const args = process.argv;
const mmap = require('mmap.js');
const shm  = require('nodeshm');
const SHM_FILE_NAME=args[args.length-1];


let fd = shm.shm_open(SHM_FILE_NAME, shm.O_RDWR, 0600);
if (fd == -1){
    console.log("FD COULD NOT BE OPENED!");
    throw "here";
}



let mm = mmap.alloc(1024, mmap.PROT_READ | mmap.PROT_WRITE, mmap.MAP_SHARED, fd, 0);


console.log("FROM NODE: ", mm.slice(0, mm.indexOf('\n')).toString('utf-8'));

Sample output:示例输出:

FROM PYTHON MAIN PROCESS:  Hello world!

FROM NODE:  Hello world!

Fortunately this doesn't work: imagine how confusing if all of the system's MAP_ANONYMOUS mappings were against the same area and kept overwriting each other.幸运的是,这行不通:想象一下,如果系统的所有MAP_ANONYMOUS映射都针对同一区域并不断相互覆盖,那该有多混乱。

Instead, use shm_open to create a new handle you can mmap in both processes.相反,使用shm_open创建一个新句柄,您可以在两个进程中进行映射。 This is a portable wrapper around the equally valid but less portable strategy of creating and mmap'ing a file in /dev/shm/ .这是围绕在/dev/shm/中创建和映射文件的同样有效但便携性较低的策略的便携包装器。

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

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