0

For example, I've got a function foo, and the caller function. I need foo to go to background, set lock file, set everything up, remove lock file. The caller must call foo and exit. I was thinking about Subprocess module, but as I see it can't do what I need from it to do. python-daemon seems to be promising, but I don't need it to run forever as a daemon.

Mark
  • 1

2 Answers2

0

Might want to look at threading

import threading

def foo(my_args):
    # do something here

def caller(some_args):
    # do some stuff
    foo_thread = threading.Thread(target=foo, args=some_args)
    foo_thread.start()
    # continue doing stuff

caller()
alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
0

You can daemonize your function in a thread: e.g.


import threading
import time

def worker(snooze):
    print(f'snoozing {snooze} seconds')
    time.sleep(snooze)


if __name__ == '__main___':

    task = threading.Thread(name='daemonize_worker', target=worker, args=(5, ))
    task.setDaemon(True)
    task.start()

This throws worker with snoozing of 5 sec in the background in a daemonized way.

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57