Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grequests with requests has collision

I am using grequests python module to call some APIs. I want to make two functions.

  1. A single request(use requests module)
  2. A multiple request(use grequests module)

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?

like image 454
Shaffron Avatar asked Apr 03 '17 11:04

Shaffron


1 Answers

Yes. Import requests after grequests. Here is an open issue about this.

like image 57
Alexey Atanov Avatar answered Nov 17 '22 04:11

Alexey Atanov