spyoungtech / grequests

Requests + Gevent = <3
https://pypi.python.org/pypi/grequests
BSD 2-Clause "Simplified" License
4.46k stars 331 forks source link

python 3.6 needs gevent select monkey patch with https requests #116

Closed jwkvam closed 6 years ago

jwkvam commented 7 years ago

With python 3.6 grequests by itself doesn't seem to query https requests asynchronously. If I comment out the monkey patch I get the following timings:

greqs 4.350938081741333
reqs  5.179100036621094

With patch_select I get these timings:

greqs 0.7194969654083252
reqs  5.032361268997192

This was the minimum change I could find that made py3.6 work, with aggressive=False grequests is still synchronous.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import grequests
import gevent.monkey
gevent.monkey.patch_select(aggressive=True)
from time import time

urls = 20 * ['https://google.com']

rs = (grequests.get(u) for u in urls)

start = time()
res = grequests.map(rs)
stop = time()

print(f'greqs {stop - start}')

start = time()
rs = [requests.get(u) for u in urls]
stop = time()

print(f'reqs  {stop - start}')
spyoungtech commented 6 years ago

I'm not sure I understand the issue and cannot reproduce. Without doing anything, it works fine in 3.6 for me.

It's a known issue that importing requests before importing grequests causes problems. Perhaps that is your issue. Have you tried reordering the imports?

Python 3.6.3 (v3.6.3:2c5fed86e0, Oct  3 2017, 00:32:08) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import grequests

In [2]: import requests

In [3]: from time import time

In [4]: urls = 20 * ['https://google.com']
   ...: 
   ...: rs = (grequests.get(u) for u in urls)
   ...: 
   ...: start = time()
   ...: res = grequests.map(rs)
   ...: stop = time()
   ...: 
   ...: print(f'greqs {stop - start}')
   ...: 
   ...: start = time()
   ...: rs = [requests.get(u) for u in urls]
   ...: stop = time()
   ...: 
   ...: print(f'reqs  {stop - start}')
   ...: 
greqs 0.5951080322265625
reqs  4.640050888061523
jwkvam commented 6 years ago

Thanks, but no, that didn't change anything for me. Maybe it's because I'm using a conda provided python? I used a different package anyway so I'm not really invested in this issue anymore.