Look at this code:
import requests
import grequests
import requests_cache
requests_cache.install_cache('bla')
urls = [
'http://www.heroku.com',
'http://python-tablib.org',
'http://httpbin.org',
'http://python-requests.org',
'http://kennethreitz.com'
]
rs = (grequests.get(u) for u in urls)
results = grequests.map(rs)
I would expect that after executing this I will find bla.sqlite
file in the current directory and executing
results = grequests.map(rs)
will be MUCH faster because data will be taken from sqlite cache. Unfortunately this is not true, file wasn't created at all and there is no speedup. When I use requests insetead of grequests everything works fine. So the question is a title says: Is it possible to make grequests and requests_cache work together? and if yes, how?
The
requests_cache.install_cache()
function patchesrequests.Session
, but you already importedgrequests
, which used:As such,
grequests
never uses the patched session object.Move the import to after you installed the cache:
Alternatively, create a
CachedSession
object and pass that in to thegrequests.get()
(and related) methods as thesession
parameter:Take into account that the cache storage backend may not be able to handle the corcurrent access safely. The
sqlite
backend uses a thread lock, for example, which might well clash.