Forums

Multiple instances of long running tasks

Hey,

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.

Thanks

P.s. there are 3 currently running in my account - happy for you to take a look if needed

Do you see any evidence that there are actually parallel tasks running (eg: it is doing parallel prints/writes to files etc), or just that you can see multiple processes in the fetch processes table? (One of my conjectures is that maybe the tasks have finished running on your end, but just haven't been fully stopped on our end yet)

Also do you spawn any subprocesses/threads? (Maybe the main thread that keeps the socket open has finished and closed but waiting for some subprocess/thread to finish)

Just via the fetch table.

No threads or sub-processes.

All 3 are still there and when I hit refresh on the table, 2 of them are incrementing the CPU - I got put into tarpit earlier due to them running.

ah- could it be that your code in the line from main import keep_running might be doing computations at a module level? ie. instead of having functions etc that don't get run immediately at import, you also have stuff that's "not indented" and is run immediately when you import it (and thus before the lock check takes place).

Perhaps try doing the import at the bottom below sys.exit()?

That was it - On import the imported file was importing other files, one of which had some un-tidied test code which was executing.

Thanks for the help