What's the equivalence of os.fork() on Windows with Python? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2021-02-18 17:51:07

问题


This code works well in Mac/Linux, but not in Windows.

import mmap
import os

map = mmap.mmap(-1, 13)
map.write("Hello world!")

pid = os.fork()

if pid == 0: # In a child process
    print 'child'
    map.seek(0)
    print map.readline()

    map.close()
else:
    print 'parent'
  • What's the equivalent function of os.fork() on Windows?

回答1:


Depending on your use case and whether you can use python 2.6 or not, you might be able to use the multiprocessing module.




回答2:


The answer here may not answer the question. The problem is due to fork(). From the example, you seemed want to share data between two Python scripts. Let me explain my view.

As of Python 3.8, there is no true Unix's fork() implementation in Windows platform. For the example above to work, a child process must inherit all environment and open file descriptors.

I understand that Windows now support Windows Linux Subsystem, but the last i checked it still does not fully implement fork. Cygwin does actually but it is a bit slow.

I do not know how, until now, to pass information between two Python scripts using mmap in Windows platform. Use

  1. multiprocessing.Queue or
  2. multiprocessing.Pipe or
  3. multiprocessing.Manager or
  4. multiprocessing's shared memory (Value and Array) instead.

I believe you could make each Python script to read in content of the to-be-mapped file into Array of characters. Then use your own structure to map the shared memory into a structured data as you do for the to-be-mapped file.



来源:https://stackoverflow.com/questions/2738809/whats-the-equivalence-of-os-fork-on-windows-with-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!