23

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
RyanBrady
  • 6,633
  • 4
  • 27
  • 32

7 Answers7

18

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.

kanaka
  • 70,845
  • 23
  • 144
  • 140
  • 3
    This is the correct answer, but because of the lack of `fork()` on Windows, there are a couple of caveats, the main one being to make sure any script is protected by a `if __name__ == "__main__"` guard if it is to call any code that does multiprocessing. The spawned process starts fresh and needs to load modules to be able to reconstruct state. – clacke Sep 13 '16 at 13:18
  • Does arguments passed to [`multiprocessing.Process()`](https://docs.python.org/3.4/library/multiprocessing.html?highlight=process#the-process-class) are serialized to pass to new process? I wrote SSL client server app. Server forks new process for each request received but was raising `TypeError: cannot serialize socket object`. Doing same with simple (non SSL) socket worked as [explained here](https://stackoverflow.com/questions/50910579/). But I am unsure as the error was originated at [`socket.py, line 185, in __getstate__`](https://github.com/python/cpython/blob/master/Lib/socket.py#L191). – Mahesha999 Jun 19 '18 at 08:09
10

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.

Community
  • 1
  • 1
Hugh Allen
  • 6,509
  • 1
  • 34
  • 44
3

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
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 8
    `subprocess` and `multiprocessing` (suggested by RJBrady) could be better alternatives to `os` and `threading`. – jfs Oct 04 '08 at 17:24
3

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_computations, args=[57, 83]).start()

    print "I'm continuing while that other function runs in another thread!"
dabla
  • 23
  • 5
Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
3

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.

RyanBrady
  • 6,633
  • 4
  • 27
  • 32
  • 2
    `multiprocessing` module is a part of the standard library. `subprocess` fits for external programs, `multiprocessing` fits for python code. – jfs Oct 04 '08 at 17:21
2

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
  • 67,947
  • 40
  • 154
  • 200
  • 1
    As of Python 2.6/3.0 (Oct and Dec 2008), `processing` is part of the standard library as `multiprocessing`. – clacke Sep 13 '16 at 13:01
0

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

ftdysa
  • 1,242
  • 6
  • 17
  • 26
  • That's what `multiprocessing` does on Windows these days. https://docs.python.org/3.5/library/multiprocessing.html?highlight=spawn#contexts-and-start-methods – clacke Sep 13 '16 at 13:06