Skip to content Skip to sidebar Skip to footer

Proper Way To Start Thread From Python Daemon

I need to write simple daemon with web interface. The idea is to use python-daemon package and run wsgiref.simple_server inside one thread. Daemon works fine with the following cod

Solution 1:

This is a limitation (discussion thread starts here) of the daemon library.

TL;DR: Your options are:

  • Don't use daemon, because it is unrepairably broken in this regard.
  • Start the thread within the "with daemoncontext" block.

Long version:

When the daemon library switches to daemon context, it does a double-fork. That means it first forks and then kills the parent process. A new fork does not have any thread, so exiting the parent process equates killing your webgui thread. Ultimately, any solution to this problem must start any permanent threads in the newly created child process. The downside of doing this in the daemon context is that you are no longer able to propagate potential errors to the user. Ideally you'd double-fork, but do not exit the parent process, then set up your daemon and right before entering the main loop make the parent process exit. This is not achievable with the daemon library or any library implementing the PEP3143 draft.

Solution 2:

Worked for me. You basically have to imagine that setting up the daemon context is like a frontal lobotomy :-)

Here is what I figured is easy to follow My main only parses arguments and, if flagged to run in the background, creates the daemon context. Thereafter, it executes a common main loop function _main_common(), which is where everything is done including all threads being created:

args = parse_arguments(args)
if args.foreground:
   # we run in the foreground, so just run the main loop
   _main_common()
else:
   # we run in the background, so now create the daemon context and run the main loop
   context = create_daemon_context(context, detach_process=True)
   with context:
      _main_common()

Post a Comment for "Proper Way To Start Thread From Python Daemon"