I am using grequests python module to call some APIs. I want to make two functions.
When I use two modules in two different files, it runs normally, but when I import two modules in the same file, requests module fall in infinity recursive.
#!/usr/bin/env python
#-*- encoding:utf-8 -*-
import requests
import grequests
def SingleRequest():
rs = requests.get("www.example.com")
return rs
def MultiRequest():
urls = [
"www.example1.com",
"www.example2.com",
"www.example3.com"
]
rs = [grequests.get(u) for u in urls]
rs_map = grequests.map(rs);
return rs_map;
If I call MultiRequest() -> do Well!
but if I call SingleRequest() ..... ↓
Exception Type: RecursionError
Exception Value: maximum recursion depth exceeded
Exception Location: /usr/local/lib/python3.6/ssl.py in options, line 459
/usr/local/lib/python3.6/ssl.py in options
super(SSLContext, SSLContext).options.__set__(self, value) X 100 times...
Is it possible to use requests and grequests in one file?
Yes. Import requests after grequests. Here is an open issue about this.
This is the only thing that worked for me (Python 3.8.6, a module needed imports of grequests and requests) (source). This should precede all other imports:
from gevent import monkey
def stub(*args, **kwargs): # pylint: disable=unused-argument
pass
monkey.patch_all = stub
import grequests