0

I have a discrete event simulation that (optionally) works with greenlets from the greenlet module. The event loop is entirely controlled by me. However, I think the gevent module is better maintained and can be compatible with pytest (which greenlet can't as far as I know). So I would just like to create a parent greenlet which gives control to child greenlets with a switch call. These child greenlets always give control back to the parent with a switch call. That's essentially it. Here's a very minimal working example:

import greenlet


def main():
    while not (child0_greenlet.dead and child1_greenlet.dead):
        child0_greenlet.switch()
        child0_greenlet.switch()
        child1_greenlet.switch()


def child0_process():
    for i in range(5):
        print("child0")
        parent_greenlet.switch()


def child1_process():
    for i in range(5):
        print("child1")
        parent_greenlet.switch()


parent_greenlet = greenlet.greenlet(main)
child0_greenlet = greenlet.greenlet(child0_process, parent_greenlet)
child1_greenlet = greenlet.greenlet(child1_process, parent_greenlet)

parent_greenlet.switch()

This works properly. But I want it to use the gevent module instead.

I can't find how to do that in gevent.

1 Answers1

0

If all you are trying to do is run threads with a master watcher, this is an example of you could do it in gevent.

import gevent

threads=[]

def main():
    gevent.joinall(threads)
    print('back to main thread')


def add_job(func):
    threads.append(gevent.spawn(func))


def example():
    print('hello')
    gevent.sleep(1)

if __name__=='__main__':
    add_job(example)
    add_job(example)
    main()
eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20