在Windows中复制fork()的最佳方法是什么?
时间:2020-03-05 18:42:15 来源:igfitidea点击:
如何实现一些逻辑,使我能够使用Python在Windows上通过fork()
系统调用来重现Linux上的功能?
我专门尝试在SAPI Com组件上执行一种方法,同时继续执行主线程中的其他逻辑而不会阻塞或者等待。
解决方案
回答
可能是python的spawn()版本吗? http://en.wikipedia.org/wiki/Spawn_(operating_system)
回答
看一下os模块中的流程管理功能。具有用于以多种不同方式(同步和异步)启动新流程的功能。
我还要注意Windows在其他系统上没有提供与fork()完全相同的功能。要在Windows上进行多处理,我们将需要使用线程模块。
回答
除了Greg指出的os模块中的流程管理代码外,我们还应该看看线程模块:https://docs.python.org/library/threading.html
from threading import Thread def separate_computations(x, y): print sum(x for i in range(y)) # really expensive multiplication Thread(target=separate_compuations, args=[57, 83]).start() print "I'm continuing while that other function runs in another thread!"
回答
我们可能还希望使用处理模块(http://pypi.python.org/pypi/processing)。它具有许多功能,可使用与线程模块相同的API编写并行系统。
回答
Eli的Threading示例将运行该线程,但在该行之后不执行任何工作。
我将研究处理模块和子过程模块。我认为我正在运行的com方法需要在另一个进程中,而不仅仅是在另一个线程中。
回答
实际上,在Windows中Cygwin下已经复制了fork(),但是它非常有毛。
The fork call in Cygwin is particularly interesting because it does not map well on top of the Win32 API. This makes it very difficult to implement correctly.
有关此黑客的说明,请参见《 Cygwin用户指南》。