Hi,
I am trying to follow your code at Long Running Tasks, but the code still allows multiple spawns of my program. Rather than pasting my entire code here, the following trivial code exemplifies what I mean. This code simply prints 'Hello" on the screen every 5 seconds until a keyboard interrupt.
Running this code for first time, works as expected. But running it again launches another instance. The so-called socket lock fails to lock, so it will run as many times I execute the code. What is going on? I have checked this forum for similar cases, but no one seems to be able to give me a clear, straightforward answer.
I appreciate any help. Thank you.
import logging
import socket
import sys
lock_socket = None
def is_lock_free():
global lock_socket
lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
try:
lock_id = "myname.testing123"
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()
import time
while True:
print('Hello...')
time.sleep(5)