Coder Social home page Coder Social logo

response is None about grequests HOT 4 CLOSED

zyc130130 avatar zyc130130 commented on July 17, 2024
response is None

from grequests.

Comments (4)

spyoungtech avatar spyoungtech commented on July 17, 2024 1

Hi @zyc130130 there may be some limits on how many open connections your client can maintain. size=50000 is probably far too large to be practical for a pool size. Generally speaking, every open socket connection requires an available file descriptor on your OS. On most Linux distributions, the default limit is 1024 or 2048 concurrent descriptors per process. This is just one of the practical limitations on how many concurrent requests you can do.

Keep in mind, the size parameter is how many concurrent greenlets will be created to handle requests. You could think of them also as "workers" if you wanted. Usually setting the size to around 200 is reasonable for most systems with average amounts of activity. You'll have to test to see what works for you. It's best to start small and work your way up to what is acceptable or stable.

So, key takeaway: lowering your size will probably solve your problem.

Does your error handler log the error? If so, could you provide the output?

Another thing that may help is utilizing a session to reuse connections for multiple requests, if that's practical for your use-case. It usually makes things much faster, too.

Without session:

In [1]: import grequests

In [2]: reqs = [grequests.get('https://httpbin.org/status/200') for _ in range(100)]

In [3]: import time

In [4]: start=time.time();grequests.map(reqs, size=10);end=time.time();print(end-start)
3.2363204956054688

With session: much faster!!

In [5]: import requests  # important! always import requests AFTER grequests

In [6]: sesh = requests.Session()

In [7]: reqs = [grequests.get('https://httpbin.org/status/200',session=sesh) for _ in range(100)]

In [8]: start=time.time();grequests.map(reqs, size=10);end=time.time();print(end-start)
1.007857322692871

😮

Also notice how I only used a pool size of 10 and was able to do about 100 req/s 😄

from grequests.

zyc130130 avatar zyc130130 commented on July 17, 2024 1

Hi @zyc130130 there may be some limits on how many open connections your client can maintain. size=50000 is probably far too large to be practical for a pool size. Generally speaking, every open socket connection requires an available file descriptor on your OS. On most Linux distributions, the default limit is 1024 or 2048 concurrent descriptors per process. This is just one of the practical limitations on how many concurrent requests you can do.

Keep in mind, the size parameter is how many concurrent greenlets will be created to handle requests. You could think of them also as "workers" if you wanted. Usually setting the size to around 200 is reasonable for most systems with average amounts of activity. You'll have to test to see what works for you. It's best to start small and work your way up to what is acceptable or stable.

So, key takeaway: lowering your size will probably solve your problem.

Does your error handler log the error? If so, could you provide the output?

Another thing that may help is utilizing a session to reuse connections for multiple requests, if that's practical for your use-case. It usually makes things much faster, too.

Without session:

In [1]: import grequests

In [2]: reqs = [grequests.get('https://httpbin.org/status/200') for _ in range(100)]

In [3]: import time

In [4]: start=time.time();grequests.map(reqs, size=10);end=time.time();print(end-start)
3.2363204956054688

With session: much faster!!

In [5]: import requests  # important! always import requests AFTER grequests

In [6]: sesh = requests.Session()

In [7]: reqs = [grequests.get('https://httpbin.org/status/200',session=sesh) for _ in range(100)]

In [8]: start=time.time();grequests.map(reqs, size=10);end=time.time();print(end-start)
1.007857322692871

😮

Also notice how I only used a pool size of 10 and was able to do about 100 req/s 😄

Hi,
Thanks for your replay!
At the beginning, I didn’t set the setting parameter "size". It means "size = +∞"? I have the problem, then I set "size=50000", the problem still occurs.
According to what you said,I try some test as fellow:
len(res_list )==3270
1.
req_list.append(grequests.post(sever_url, data=data_temp))
res_list = grequests.map(req_list, exception_handler=err_handler, size=1000)
when use code as 1, I got the right response which has no "None".
2.
req_list.append(grequests.post(sever_url, data=data_temp, session=sesh))
res_list = grequests.map(req_list, exception_handler=err_handler, size=1000)
when use code as 2, the same problem is occured. So i think the reason is "size".
3.
req_list.append(grequests.post(sever_url, data=data_temp, session=sesh))
res_list = grequests.map(req_list, exception_handler=err_handler, size=100)
when use code as 3, I got the right response which has no "None".

Then I compare the results of whether to use the parameter session as code 4,
4.
req_list.append(grequests.post(sever_url, data=data_temp, session=sesh))
res_list = grequests.map(req_list, exception_handler=err_handler, size=10)
when using parameter "session=sesh", program running time is 5:24s,
when not using parameter "session=sesh", program running time is 5:23s
The increase in program speed is not obvious, it seem to be not working.

My gunicorn.conf file set as fellow:
workers = 4
worker_class = 'gevent'

from grequests.

AustinGilkison avatar AustinGilkison commented on July 17, 2024

Also having the same issue.
urls only contains one URL at the moment and still doesn't work.

rs = (grequests.get(u) for u in urls)
results = grequests.map(rs, size=10)

Results is a list of None.

from grequests.

spyoungtech avatar spyoungtech commented on July 17, 2024

Almost always this is because there is some error happening during processing of the request.

For example:

>>> import grequests
>>> rs = [grequests.get('badscheme://foobar.com')] * 5
>>> grequests.map(rs)
[None, None, None, None, None]

If you use an exception handler, you can see what is going wrong.

>>> def exception_handler(request, exception):
...    print("Request failed", exception)
>>> rs = [grequests.get('badscheme://foobar.com')] * 5
>>> grequests.map(rs, exception_handler=exception_handler)
Request failed No connection adapters were found for 'badscheme://foobar.com'
Request failed No connection adapters were found for 'badscheme://foobar.com'
Request failed No connection adapters were found for 'badscheme://foobar.com'
Request failed No connection adapters were found for 'badscheme://foobar.com'
Request failed No connection adapters were found for 'badscheme://foobar.com'
[None, None, None, None, None]

from grequests.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.