Python, Gevent and Multicore

Yes half of the programmers in the world knows Python can't run on multiple cores because of the famous GIL.

Half of the Python backend programmers knows there is the "high-performance networking library" called Gevent.

So, if a multi-core server at your disposal to build a multi-user web service, what are your choices?

I/O bounded

If the service is I/O bounded i.e. light in CPU, possibility involves calling other services, etc. Gevent is right for this. Gunicorn / uWSGI all allows running multiple worker processes; use them to fill up your cores and increase the co-routine count (for uWSGI) to make each core (each Gevent loop) handle enough concurrent clients. This is what Gevent is good at.

CPU bounded

If a lot of processing is involved for some of the requests, don't let them run in the Gevent process. Once a request occupies the CPU, no other greenlets could handle any requests. Usual, simple solution is to let threads do the task, however Python is famous for having a non-functioning multi-threading model. Use GIPC to command a background process to do the heavy lifting so the event loop could continue serve other easy going customers.

If all requests are CPU bounded, better not do ad-hoc forkings. Either distribute the processing to some work farm with Celery or lighter-weight handcrafted solutions using Redis / Thrift, etc, or just use Nginx to reverse proxy to a number of Python nodes, with or without Gevent.

Bonus:

One stupid thing I did to myself:
I was running a kind of reverse proxy app on top of Gevent with monkey-patching. Some of the client requests requires an array of requests to upstream server. I wrote, with my eyes closed, this:
resps = [requests.get(u) for u in upstream_urls]
reduce (proc, resps)
And then I figured I'm dead. The running time of the first one is linear to number of requests where Gevent provided absolutely nothing helpful. Using grequests.map did the trick but I was wondering why.

The solution is so simple. Each "requests.get" call is evaluated by Gevent one-by-one, so Gevent is purposely waiting for the previous to finish before starting the next. It is still switching to other Greenlets if there are other requests at the same time, but this list of request are not sent to Gevent at the same time at all.

resps = grequests.map([grequests.get(u) for u in upstream_urls])
is definitely the simple solution.



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值