gevent, ways of triggering the newly-spawn tasks to run

820 views Asked by At

my project is using gevnet, which is new to me, to implement a thrift server.

I am reading the code and learning from its document. The code snippet below is in my project:

TSocket.socket = gevent.socket # I find this when comparing the production
                               # code and mine. It seems that this socket
                               # schedule the tasks when there are newly-spawn ones. 
                               # Could someone tell me more about it?

while True:
    try:
        client = self.serverTransport.accept()
        gevent.spawn(self._serve_client, client)
    except (SystemExit, KeyboardInterrupt):
        break
    except Exception as x:
        logging.exception(x)

After spawning, it directly finishes it work here.

But in my own implementation, which is for self-learning, i have to do the following:

while True:
    try:
        client = self.serverTransport.accept()
        gevent.spawn(self._serve_client, client)
        gevent.sleep(0)  # switch to the newly-spawn task.
                         # this makes it to process tasks one by one and
                         # this makes it non-concurrent
    except (SystemExit, KeyboardInterrupt):
        break
    except Exception as x:
        logging.exception(x)

In my production code, i haven't found any clue of how to trigger the tasks to run. So I am asking here for some light on the ways of running newly-spawn tasks in a server above.

1

There are 1 answers

4
mguijarr On

In your code, you have to call gevent.sleep(0) because nothing else triggers the gevent loop. Having this sleep gives control back to gevent, which executes the spawned greenlet.

The line TSocket.socket = gevent.socket in the production code patches the default thrift socket implementation to use a gevent socket instead; gevent sockets run the gevent loop, so you really need this patch for your code to run.

Without the patch, TSocket.socket is blocking and kills concurrency (without OS-level threads).