Question

I'm making a library that uses gevent to do some work asynchronously. I'd like to guarantee that the work is completed, even if the main module finishes execution.

class separate_library(object):
    def __init__(self):
        import gevent.monkey; gevent.monkey.patch_all()

    def do_work(self):
        from gevent import spawn
        spawn(self._do)

    def _do(self):
        from gevent import sleep
        sleep(1)
        print 'Done!'

if __name__ == '__main__':    
    lib = separate_library()
    lib.do_work()

If you run this, you'll notice the program ends immediately, and Done! doesn't get printed.

Now, the main module doesn't know, or care, how separate_library actually accomplishes the work (or even that gevent is being used), so it's unreasonable to require joining there.

Is there any way separate_library can detect certain types of program exits, and stall until the work is done? Keyboard interrupts, SIGINTs, and sys.exit() should end the program immediately, as that is probably the expected behaviour.

Thanks!

Was it helpful?

Solution

Try using a new thread that is not a daemon thread that spawns your gevent threads. Your program will not exit due to this non daemon thread.

import gevent
import threading

class separate_library(object):
    def __init__(self):
        import gevent.monkey; gevent.monkey.patch_all()

    def do_work(self):
        t = threading.Thread(target=self.spawn_gthreads)
        t.setDaemon(False)
        t.start()


    def spawn_gthreads(self):
        from gevent import spawn
        gthreads = [spawn(self._do,x) for x in range(10)]
        gevent.joinall(gthreads)

    def _do(self,sec):
        from gevent import sleep
        sleep(sec)
        print 'Done!'

if __name__ == '__main__':    
    lib = separate_library()
    lib.do_work()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top