简体   繁体   中英

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.

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. In general, in bash (as well as most other shells) if you append a & to your command it runs the command in the background.

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. But there's a whole existing thread dedicated to this issue: Calling an external command in 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:

* 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.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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