Why the amount of greenlets will impact the elapse

2019-08-23 03:17发布

问题:

I'm using Python coroutine library gevent and monkey patch to increase the concurrency of http requests. But I noticed the elapsed time of the responses increased while the concurrency increased. Below the sample code:

import gevent
from gevent import monkey
import requests

monkey.patch_all(thread=False)


def action():
    resp = requests.get("https://www.google.com")
    if resp.status_code == 200:
        print resp.elapsed.total_seconds()


jobs = []
for i in range(100):
    jobs.append(gevent.spawn(action))

gevent.joinall(jobs)

When 10 greenlets were spawned, the elapsed time was around 0.9 seconds, but when the greenlets number was increased to 100, the elapsed time was around 1.6 ~ 2.0 seconds. Why this happened?

回答1:

greenlets are still single threaded, meaning they can only do one thing at a time. For any process that is cpu intensive this will incur a delay. Yes it is asynchronous, but it is not multiprocessed, so if something uses 1 second of CPU, you have delayed the results of any subsequent greenlet by 1 second.

So as your queue grows, the delay even if just ms, becomes noticeable.