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博客 ” ,链接:https://blog.itpub.net/301743/viewspace-737271/
,如需转载,请注明出处,否则将追究法律责任。