What's the best way to duplicate fork() in win

2020-01-24 09:32发布

How do I implement some logic that will allow me to reproduce on Windows the functionality that I have on Linux with the fork() system call, using Python?

I'm specifically trying to execute a method on the SAPI Com component, while continuing the other logic in the main thread without blocking or waiting.

7条回答
该账号已被封号
2楼-- · 2020-01-24 09:47

Possibly a version of spawn() for python? http://en.wikipedia.org/wiki/Spawn_(operating_system)

查看更多
干净又极端
3楼-- · 2020-01-24 09:48

Use the python multiprocessing module which will work everywhere.

Here is a IBM developerWords article that shows how to convert from os.fork() to the multiprocessing module.

查看更多
走好不送
4楼-- · 2020-01-24 09:49

In addition to the process management code in the os module that Greg pointed out, you should also take a look at the threading module: 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!"
查看更多
劫难
5楼-- · 2020-01-24 09:54

Have a look at the process management functions in the os module. There are function for starting new processes in many different ways, both synchronously and asynchronously.

I should note also that Windows doesn't provide functionality that is exactly like fork() on other systems. To do multiprocessing on Windows, you will need to use the threading module.

查看更多
时光不老,我们不散
6楼-- · 2020-01-24 09:59

The Threading example from Eli will run the thread, but not do any of the work after that line.

I'm going to look into the processing module and the subprocess module. I think the com method I'm running needs to be in another process, not just in another thread.

查看更多
霸刀☆藐视天下
7楼-- · 2020-01-24 10:00

fork() has in fact been duplicated in Windows, under Cygwin, but it's pretty hairy.

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.

See the The Cygwin User's Guide for a description of this hack.

查看更多
登录 后发表回答