2

Ok, so this may be a little bit unorthodox or I'm just stupid or both :)

I'm trying a very simple setup where I start a bottle server in one Process instance and start a smallish TFTP server in another instance.

#!/usr/bin/env python
import bottle
import sys
import tftpy
from multiprocessing import Process

def main():
    try:
        t = Process(target=bottle.run(host='0.0.0.0', port=8080))
        t.daemon = True
        t.start()
        t.join()
        h = Process(target=tftpy.TftpServer('/srv/tftp').listen('0.0.0.0', 69))
        h.start()
        h.join()

    except KeyboardInterrupt:
        sys.stdout.write("Aborted by user.\n")
        sys.exit(1)

if __name__ == "__main__":
    main()

Unless I'm totally crazy, I'd expect them to start up in parallel. In reality, what happens is that bottle starts and locks whole thing up. If I exit bottle, TFTP daemon starts.

I also tried a similar approach with threading module, with about same results.

What am I doing wrong?

2 Answers 2

6

There are several issues:

  • you call run() in the main thread. You should pass arguments in args instead:

    Process(target=bottle.run, kwargs=dict(host='0.0.0.0', port=8080))
    
  • you call t.join() which blocks until t process ends before h.start(). Join after all processes are started instead

  • bottle, tftpy might not be compatible with multiprocessing module. You could try subprocess module if so
3
  • Calling run on the main thread was the issue. Thanks man, you opened my eyes to something very obvious which I was just overlooking! :)
    – favoretti
    Commented Oct 23, 2012 at 0:16
  • 1
    As a side note: Both bottle and tftp are IO-bound, not CPU-bound. Threading should be enough, no need to spawn additional processes.
    – defnull
    Commented Oct 23, 2012 at 9:04
  • @defnull: threads are harder to kill without cooperation from them, they don't release resources properly.
    – jfs
    Commented Oct 23, 2012 at 11:17
0

Well, I am not sure if I understood what you are trying to accomplish but if I were in your place I would try to use the python-daemon package

I think that both bottle and TFTP could be daemonized. As you are only in search for a simple test I guess that the examples given in the python-daemon webpage would be enough.

If you really like the idea of going daemonizing things, I would suggest also that you search about the proper daemonizing approach for your platform as this way you have several facilities to manage your daemon by making them more alike to the ones found in your OS.

For some simple examples: http://troydhanson.wordpress.com/2012/08/21/minimal-sysvinit-launchd-and-upstart/

3
  • Pardon me if I was unclear. I will daemonize indeed. But not those 2 processes. I'm writing my own tool that will embed both bottle and TFTP and that tool I will indeed daemonize with python-daemon. I was merely looking for a solution to run those two as separate threads/subprocesses inside my own daemon.
    – favoretti
    Commented Oct 23, 2012 at 0:23
  • Should I simply delete this answer as it doesn't add anything to the problem? Commented Oct 23, 2012 at 0:25
  • Feel free to leave it :) It probably adds a bit more info on daemonizing in python in general.
    – favoretti
    Commented Oct 23, 2012 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.