0

I'm trying to write a scheduler script for slurm jobs in python.

Basically, I have a script contained in a function,let's call it job_array_scheduler(), that contains a time.sleep() loop. This is expected to run for a day or more. I want to be able to start this function off as a separate thread from inside python, such that I can then exit the session and this thing will keep running.

I tried doing it like so:

import threading
scheduler_thread = threading.Thread(target=job_array_scheduler, args=args, daemon=True)
scheduler_thread.start()

but this exits when I exit the python session (i.e just with exit).

How to I get around this, to keep the above thread alive so I can exit the python session or even log my user off?

The only way I can think of is writing another script to wrap around the above, so I can run it in the background from the shell doing something like python wrapper_script.py & but I would prefer to avoid that as I would have to write a separate outer script for each such job I want to do.

Marses
  • 1,464
  • 3
  • 23
  • 40
  • Use processes with `multiprocessing` instead of threads (which, in python, don't work as you expect anyway thanks to the GIL) – GPhilo Sep 22 '17 at 11:31

1 Answers1

0

Don't exit the process (threads are part of the process and cannot outlive it). What you want is to daemonize (or detach) the process.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76