26

os.fork() command is not supported under windows, and gives the following error:

AttributeError: 'module' object has no attribute 'fork'

So the general question is How to run a script that contain a call to os.fork() under Windows?. I don't mind using something that only mocks up the behavior and runs much slower, it's only for testing. I also prefer not to change the script as it is a 3rd party module.

To give you a wider perspective, I'm trying to use the module rq a.k.a redis queue on Windows. Eventually I will run the code on heroku server which is a Linux machine, but during the development of the web app I'm using Windows.

zenpoy
  • 19,490
  • 9
  • 60
  • 87
  • 1
    If you're planning to deploy on Linux, it'll be easier to develop on Linux. If you can't/won't install Linux, then at least install [Cygwin](http://www.cygwin.com/) on your Windows box, which will allow you to install a version of Python which supports `os.fork()`. – Aya Jun 25 '13 at 15:08
  • @Aya - this may sound easy, though I'm not sure how to move the whole 'heroku', 'git', 'foreman', 'virtualenv' to use a different python version. How easy is it? Can you elaborate? – zenpoy Jun 25 '13 at 15:12
  • @zenpoy "Version" here doesn't refer to a different version number, but to a different build. A Python built for Cygwin would likely work with heroku, git, etc. just fine. The only question is, given your lack of experience with Cygwin, how many other problems you'd run into in such a setup. – user4815162342 Jun 25 '13 at 15:15
  • @zenpoy Well, you may have to frob your `PATH` environment variable so it picks up the right version, e.g. `C:\cygwin\bin\python.exe` rather than `C:\Program Files\Python\python.exe`. – Aya Jun 25 '13 at 15:16

3 Answers3

12

There is no easy way to emulate fork() on systems that don't have it, such as Windows. If the code only uses fork() to start a new process with exec, you can port it to use subprocess. But this doesn't appear to be the case in rq, so you have several options:

  1. Port rq to Windows, or ask someone to do it for you. The easiest way to port the part of the code that calls fork() might be by using the multiprocessing module. However, you will still need to replace other parts of the code that depend on Unix, such as uses of signal.alarm() in the timeouts module.

  2. Use Python under Cygwin, which emulates a fully functional (though slowish) fork(), so Cygwin Python has a working os.fork(). Note that to get os.fork(), you will need to use a Cygwin-built Python, such as the one that Cygwin ships, and not simply run the normal Windows Python under Cygwin.

  3. Test the application on a Linux running in a virtual machine.

Among these unhappy options I'd recommend the last one.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
1

On windows, you can install cygwin with python. This python installation will have os module which will support os.fork() call.

Nitin_k29
  • 341
  • 1
  • 3
  • 7
-1

Import a wrapper that conditionally wraps the appropriate subprocess call in a function called fork and adds it to os namespace.

Possibly it may be an option to contact the vendor and point out that os.fork is not a preferred option for you.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73