Form 1
Save a file somewhere, and you can check if the process is running if pid already exists in the file. Note that you will need to delete the file after running.
Save the Process ID in a temporary file. And put to check if this file exists at the beginning of the program besides erasing the file when the process is terminated.
import os
import sys
pid = str(os.getpid())
pidfile = "/tmp/mydaemon.pid"
if os.path.isfile(pidfile):
print ("Processo já existe e não será executado novamente")
sys.exit(-1)
else:
#seu programa aqui
facaAlgo()
os.unlink(pidfile)
Form 2
By using having
from tendo import singleton
me = singleton.SingleInstance() # roda sys.exit(-1) se existe outra instância.
How to install the Tendo library:
easy_install tendo
pip install tendo
manualmente pelo site: http://pypi.python.org/pypi/tendo
how to run an application using this form of Singleton??
– britodfbr
@britodfbr means through the library or using the PID?
– Mansueli