Searching on Google reveals x2 code snippets. The first result is to this code recipe which has a lot of documentation and explanation, along with some useful discussion und
Though you may prefer the pure Python solution provided by the python-daemon module, there is a daemon(3)
function in libc
-- at least, on BSD and Linux -- which will do the right thing.
Calling it from python is easy:
import ctypes
ctypes.CDLL(None).daemon(0, 0) # Read the man-page for the arguments' meanings
The only remaining thing to do is creation (and locking) of the PID-file. But that you can handle yourself...