> On Jan 20, 2019, at 07:37, Rex East <
rex.eas...@gmail.com> wrote:
>
> Hi, I'm reading the intro documentation for gevent, which says "Using the standard socket module inside greenlets makes gevent rather pointless". However, I wrote a little performance test using requests, and found a big advantage to gevent. I also added grequests to the comparison. With the below code, test_sync_gevent and test_async_gevent both run in about 4 seconds consistently, whereas test_sync takes 20 seconds. Am I missing something?
"Using the standard socket module" means just that: 'import socket' from the standard library (without monkey-patching). 'import socket' will produce objects that block the event loop (are non-cooperative) and hence don't allow greenlets to run concurrently. 'from gevent import socket' (or monkey-patching) will, on the other hand, produce cooperative objects that allow greenlet concurrency.
That said, I don't think you're measuring what you think you're measuring here. It happens that 'import grequests' *implicitly monkey-patches* when you import it (
https://github.com/kennethreitz/grequests/blob/master/grequests.py#L21).
Thus after that 'import socket' is equivalent to 'from gevent import socket' and 'requests.get()' is equivalent to 'grequests.get()' --- everything is cooperative with gevent. So what you're measuring here in all cases is simply the difference of running things concurrently vs sequentially. You're not measuring using standard (non-cooperative) sockets inside greenlets; you'd have to drop the import of grequests to do that.
If I do that on my system (remove 'import grequests', simply use 'requests'), I find all three take 20s as expected (all three are being run sequentially). If I then add 'from gevent import monkey; monkey.patch_all()' to the beginning of the script and run again, I see the same behaviour you report; 'test_sync_gevent' and 'test_async_gevent' both run in ~4s (because they run concurrently) while 'test_sync' still takes 20s (running sequentially).
~Jason