Make sure only one instance of a Python script is running at a time on Linux
If you have a resource intensive process that could bring your server to its knees if more than one instance is running at a time, you'll need some safeguards in place to ensure that never happens. The most "unixy" way to do this is using a process id file, or pid. One advantage of this method is that you don't have to rely on a single process to keep running and looking for work to do.
An example implementation in Python is below. Place this code at the top of a python script thats run either through an "os.system" call or cron job. The script will look for an existing pid file. If it exists it will read the process id from the file. It will then look at the process information exposed by the Linux file system and see if that process id is running the same script that I want to run. If it is then gracefully exit. Otherwise it will get the current process ID and write it to the pid file. Here's the code:
pid_name = __file__+".pid" running_pid = None if os.path.isfile(pid_name): running_pid = int(file(pid_name, "r").read()) if os.path.exists("/proc/%s" % running_pid): if __file__ in file("/proc/%s/cmdline" % running_pid).read(): sys.exit(0) curr_pid = os.getpid() pid_file = file(pid_name, "w") pid_file.write("%s" % curr_pid) pid_file.flush() pid_file.close()
Remote python debugging for free
Sometimes you need to debug a production django install if something weird is happening that isn't happening on your development box. A simple way to do this is use winpdb. First decide where you want to break your code and insert the following line:
import rpdb2; rpdb2.start_embedded_debugger("password")
The script will wait at that point for winpdb to connect. Next open winpdb and select "Attach" from the file menu. Enter the same password and the host of the webserver and start debugging away. Also you may need to pay attention to this note from the winpdb online docs:
Firewall Ports (For remote debugging)
When using Winpdb for remote debugging make sure any firewall on the way has TCP port 51000 open. Note that if port 51000 is taken Winpdb will search for an alternative port between 51000 and 51023.