Threads Versus Greenlets in Python Networking Library Gevent

jieforest發表於2012-07-27
In a previous post, I gave an introduction to gevent to show some of the benefits your application might get from using gevent greenlets instead of threads. Some people, however, took issue with my benchmark code, saying that the threaded example was contrived. In this post, I'll try to answer some of the objections.

(It actually turns out that there was a bug in the version of ab I was using to test, as well, so I re-ran the tests from the previous post, too.)

Threads versus Greenlets

Initially, I had proposed a dummy webserver that handled incoming requests by creating a thread and delegating communication to that thread. The code in question is below:

CODE:

01.def threads(port):
02.s = socket.socket()
03.s.bind(('0.0.0.0', port))
04.s.listen(500)
05.while True:
06.cli, addr = s.accept()
07.t = threading.Thread(target=handle_request, args=(cli, time.sleep))
08.t.daemon = True
09.t.start()

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-737271/,如需轉載,請註明出處,否則將追究法律責任。

相關文章