python 的生成过程
我生成一个从 Web 应用程序运行很长时间的脚本,如下所示:
os.spawnle(os.P_NOWAIT, "../bin/producenotify.py", "producenotify.py", "xx",os.environ)
该脚本已成功生成并运行,但在它结束之前我无法释放 Web 应用程序或其他应用程序使用的端口话说我无法重新启动网络应用程序。 我如何生成一个进程并使其完全独立于网络应用程序?
这是在linux操作系统上。
im spawning a script that runs for a long time from a web app like this:
os.spawnle(os.P_NOWAIT, "../bin/producenotify.py", "producenotify.py", "xx",os.environ)
the script is spawned successfully and it runs, but till it gets over i am not able to free the port that is used by the web app, or in other words i am not able to restart the web app. how do i spawn off a process and make it completely independent of the web app?
this is on linux os.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 @mark 所澄清的那样,它是一个 Linux 系统,该脚本可以轻松地使自己完全独立,即一个守护进程,遵循这个 食谱。 (您也可以在
os.fork
之后在父进程中执行此操作,然后仅在os.exec...
子进程中执行此操作)。编辑:为了澄清 @mark 对我的答案的评论的一些细节:不需要超级用户权限来按照食谱“守护”进程,也不需要更改当前工作目录(尽管Recipe 确实做到了这一点以及更多,这不是关键部分 - 相反,它是
fork
、_exit
和setsid
调用的正确逻辑顺序。 各种os.exec...
变体不以e
结尾,使用父进程的环境,因此这部分也很简单 -请参阅 Python 在线文档。为了解决其他人的评论和答案中提出的建议:我相信
subprocess
和multiprocessing
本身不会守护子进程,这似乎是 @mark 所需要的; 脚本可以自己完成,但由于一些代码必须执行fork
和setsid
,所以对我来说保留所有代码似乎更简洁在该低级平面上生成,而不是在操作过程中混合一些高级和一些低级代码。下面是上述 URL 中的配方的大幅缩减和简化版本,专门用于在父级中调用以生成守护进程子级 - 这样,代码也可以用于执行非 Python 可执行文件。 正如所给出的,代码应该满足@mark解释的需求,当然它可以通过多种方式进行定制——我强烈建议阅读原始配方及其评论和讨论,以及它推荐的书籍,以获取更多信息。
As @mark clarified it's a Linux system, the script could easily make itself fully independent, i.e., a daemon, by following this recipe. (You could also do it in the parent after an
os.fork
and only thenos.exec...
the child process).Edit: to clarify some details wrt @mark's comment on my answer: super-user privileges are not needed to "daemonize" a process as per the cookbook recipes, nor is there any need to change the current working directory (though the code in the recipe does do that and more, that's not the crucial part -- rather it's the proper logic sequence of
fork
,_exit
andsetsid
calls). The variousos.exec...
variants that do not end ine
use the parent process's environment, so that part is easy too -- see Python online docs.To address suggestions made in others' comments and answers: I believe
subprocess
andmultiprocessing
per se don't daemonize the child process, which seems to be what @mark needs; the script could do it for itself, but since some code has to be doingfork
s andsetsid
, it seems neater to me to keep all of the spawning on that low-level plane rather than mix some high-level and some low-level code in the course of the operation.Here's a vastly reduced and simplified version of the recipe at the above URL, tailored to be called in the parent to spawn a daemon child -- this way, the code can be used to execute non-Python executables just as well. As given, the code should meet the needs @mark explained, of course it can be tailored in many ways -- I strongly recommend reading the original recipe and its comments and discussions, as well as the books it recommends, for more information.
您可以使用多处理库来生成进程。 这里显示了一个基本示例:
You can use the multiprocessing library to spawn processes. A basic example is shown here: