如何在Windows上使用os.fork运行python脚本?

编程入门 行业动态 更新时间:2024-10-09 21:29:07
本文介绍了如何在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.

为了给您更广阔的视野,我尝试使用 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:

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

  • 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脚本?

    本文发布于:2023-07-06 07:45:53,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1047474.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:脚本   如何在   Windows   os   fork

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!