views:

2546

answers:

6

How do I implement some logic that will allow me to duplicate the functionality on windows that I have on linux with fork() 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.

A: 

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

Magic Hat
+4  A: 

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.

Greg Hewgill
`subprocess` and `multiprocessing` (suggested by RJBrady) could be better alternatives to `os` and `threading`.
J.F. Sebastian
+1  A: 

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: http://docs.python.org/lib/module-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!"
Eli Courtwright
+1  A: 

You might also like using the processing module (http://pypi.python.org/pypi/processing). It has lot's of functionality for writing parallel systems with the same API as the threading module...

Daren Thomas
+1  A: 

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.

RJBrady
`multiprocessing` module is a part of the standard library. `subprocess` fits for external programs, `multiprocessing` fits for python code.
J.F. Sebastian
+3  A: 

fork() has in fact been duplicated in Windows, 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 Architecture, section 5.6. "Process Creation" for a description of this hack.

Hugh Allen