如何在Windows上使用os.fork运行python脚本? [英] how to run python script with os.fork on windows?

查看:530
本文介绍了如何在Windows上使用os.fork运行python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

os.fork()命令,并显示以下错误:

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

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

因此常见的问题是如何在Windows下运行包含对 os.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.

为了给您更广阔的视野,我尝试使用<$ c $模块c> rq 在Windows上也称为 redis队列。最终,我将在Linux上的heroku服务器上运行代码,但是在Web应用程序的开发过程中,我正在使用Windows。

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.

推荐答案

在没有Windows®的系统上没有简单的方法来模拟 fork()。如果代码仅使用 fork()通过 exec 启动新进程,则可以将其移植为使用子进程。但这在看来并非如此 rq,因此您有几种选择:

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. 端口 rq 到Windows ,或请别人为您做。移植调用 fork()的部分代码的最简单方法可能是使用 multiprocessing 模块。但是,您仍然需要替换依赖Unix的代码的其他部分,例如在 timeouts <中使用 signal.alarm() / code>模块。

  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.

在Cygwin下使用Python,它可以模拟功能齐全(虽然较慢)的 fork(),因此Cygwin Python具有有效的 os.fork()。请注意,要获取 os.fork(),您将需要使用Cygwin内置的Python,例如Cygwin附带的Python,而 not 只需在Cygwin下运行普通的Windows Python。

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.

在虚拟机上运行的Linux上测试应用程序。

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

在这些不愉快的选择中,我推荐最后一个。

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

这篇关于如何在Windows上使用os.fork运行python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆