简体   繁体   English

如何在后台运行Python程序?

[英]How to run Python programs in the background?

I have a Python program that interfaces with the browser, and now I meed to make that as my background process, so that every time I click the button, the program should start running in the background. 我有一个与浏览器接口的Python程序,现在我将其作为我的后台进程,因此每次单击按钮时,程序都应该开始在后台运行。

Can anyone help me with an easy solution for this? 任何人都可以帮我解决这个问题吗?

If you're on a Unix-like system (eg Mac OSX, Linux), the command is python myscript & , which runs the command in the background. 如果您使用的是类Unix系统(例如Mac OSX,Linux),则命令为python myscript & ,它在后台运行命令。 In general, in bash (as well as most other shells) if you append a & to your command it runs the command in the background. 通常,在bash(以及大多数其他shell)中,如果向命令附加& ,它将在后台运行命令。

Depending on the way your script is launched, backgrounding with & may not necessarily work since there's a non-login shell that might be what's launched - and it would terminate when your browser session terminates. 根据脚本的启动方式,使用&可能不一定有效,因为有一个非登录shell可能是已启动的 - 并且它将在您的浏览器会话终止时终止。 But there's a whole existing thread dedicated to this issue: Calling an external command in Python 但是有一个完整的现有线程专门解决这个问题: 在Python中调用外部命令

What you want to do is spawn either a separate process, or perhaps create a daemon that's started by your browser click. 您想要做的是生成一个单独的进程,或者创建一个由浏览器单击启动的守护进程。

nohup python filename.py &

如果你使用的是Windows,这可能会有所帮助: 创建一个python win32服务

You can use http://wwwsearch.sourceforge.net/mechanize/faq.html or use http://curl.haxx.se/libcurl/python/ or use: 您可以使用http://wwwsearch.sourceforge.net/mechanize/faq.html或使用http://curl.haxx.se/libcurl/python/或使用:

* Creating your own HTTP requests using urllib2 standard python library
* Using a more advanced library that provides the capability to navigate through a websit simulating a browser such as  mechanize.

but the "trigger" which will fire the script on Linux, is what YOU have to develop. 但是你需要开发的是在Linux上激活脚本的“触发器”。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM