I've got a long running task which always seems to spawn multiple instances...
The code for this is:
import logging import socket import sys from main import keep_running lock_socket = None # we want to keep the socket open until the very end of # our script so we use a global variable to avoid going # out of scope and being garbage-collected def is_lock_free(): global lock_socket lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) try: lock_id = "griangerkid.keep_running_1" # this should be unique. using your username as a prefix is a convention lock_socket.bind('\0' + lock_id) logging.debug("Acquired lock %r" % (lock_id,)) return True except socket.error: # socket already locked, task must already be running logging.info("Failed to acquire lock %r" % (lock_id,)) return False if not is_lock_free(): sys.exit() keep_running()
This is taken from the sample code in the PA wiki.
Is there something I've done wrong? My understanding was it would not start the process if one was already running.
P.s. there are 3 currently running in my account - happy for you to take a look if needed