Python多进程中,fork之后是否可以创建共享内存?

3
我认为答案是否定的。(在这里问题的评论中看到一个假设。) 但如果能够在进程fork后构建一个新的共享(原始)数组,可能使用管道/队列/管理器来处理设置,那将是非常好的。我对操作系统不太了解;有没有可能发生这种情况?
有没有什么聪明的解决方法(也许是memmap?),可以提供与真正的共享数组相同的读写速度?

哎呀,这个问题已经在多年前得到回答了......一定要提高搜索技能! - Adam S.
1个回答

2

我认为可以通过在现有文件上共享内存映射来完成。只需运行下面的示例多次并观察输出即可。一旦所有进程都打开了共享文件,就可以删除磁盘上的文件并继续使用共享内存。这里使用了文件锁,但可能不是最佳方法。

#!/usr/bin/env python3

import fcntl
import mmap
import os
import time
from contextlib import contextmanager, suppress

# Open (and create if necessary) a file of shared_size.
# Every sleep_timeout seconds:
#   - acquire an exclusive lock,
#   - read the data, and
#   - write new data

# 1kiB for example
shared_size = 1024
filename = "shared_data.bin"
sleep_timeout = 1

# Context manager to grab an exclusive lock on the
# first length bytes of a file and automatically
# release the lock.
@contextmanager
def lockf(fileno, length=0):
  try:
    fcntl.lockf(fileno, fcntl.LOCK_EX, length)
    yield
  finally:
    fcntl.lockf(fileno, fcntl.LOCK_UN, length)

def ensure_filesize(f, size):
  # make sure file is big enough for shared_size
  f.seek(0, os.SEEK_END)
  if f.tell() < size:
    f.truncate(size)

def read_and_update_data(f):
  f.seek(0)
  print(f.readline().decode())
  f.seek(0)
  message = "Hello from process {} at {}".format(os.getpid(), time.asctime(time.localtime()))
  f.write(message.encode())

# Ignore Ctrl-C so we can quit cleanly
with suppress(KeyboardInterrupt):
  # open file for binary read/write and create if necessary
  with open(filename, "a+b") as f:
    ensure_filesize(f, shared_size)

    # Once all processes have opened the file, it can be removed
    #os.remove(filename)

    with mmap.mmap(f.fileno(), shared_size) as mm:
      while True:
        with lockf(f.fileno(), length=shared_size):
          read_and_update_data(mm)
        time.sleep(sleep_timeout)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接