ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Threads Versus Greenlets in Python Networking Library Gevent

Threads Versus Greenlets in Python Networking Library Gevent

原创 Linux操作系统 作者:jieforest 时间:2012-07-27 11:44:22 0 删除 编辑
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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-04-23

  • 博文量
    443
  • 访问量
    508588