Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Too many open files" error, regardless of size parameter #54

Closed
david-cliqz opened this issue May 27, 2014 · 8 comments
Closed

"Too many open files" error, regardless of size parameter #54

david-cliqz opened this issue May 27, 2014 · 8 comments

Comments

@david-cliqz
Copy link

I am using grequests to test the performance of my service. Both the service and the machine from which I start the requests are inside Amazon EC2. It seems that I cannot start more requests than ulimit -n, even though I explicitly specify the size parameter for map() / imap()

My setup is roughly like this:

headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
params = [json.dumps({'url': urls[i]}) for i in urls]
reqs = [grequests.post(SERVICE_URL, data=params[i % len(params)], headers=headers)
            for i in xrange(no_requests)]
res = grequests.imap(reqs, size=200)
for r in res:
    ...
    r.close()

Doesn't the size parameter here tell grequests that it should issue no_requests, 200 at a time?

@rtdean
Copy link

rtdean commented Jun 20, 2014

ulimit -n shows the soft/hard limits for the number of open file descriptors a process can hold. Once you hit that limit, you can't open up any more FD's... this includes network sockets.

One of the things you could do is build a requests.Session object, and either use the default HTTPAdapter or mount a new one with a larger connection pool. Then, grequests/requests will end up doing HTTP Pipelining, reusing the existing connections where possible.

Try something like:

import requests
import grequests

session = requests.Session()
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
params = [json.dumps({'url': urls[i]}) for i in urls]
reqs = (grequests.post(SERVICE_URL, data=params[i % len(params)], headers=headers, session=session)
            for i in xrange(no_requests))
for r in grequests.imap(reqs, size=200):
    ...

if you call r.close() in your for loop, you end up closing the underlying connection, and forcing a new connection to be established. (If you aren't using a Session, this ends up happening anyways).

You can also mount a new HTTPAdapter with a larger connection pool:

import requests
from requests.adapters import HTTPAdapter
import grequests

session = requests.Session()
session.mount('http://', HTTPAdapter(pool_connections=50, pool_maxsize=50))
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
params = [json.dumps({'url': urls[i]}) for i in urls]
reqs = (grequests.post(SERVICE_URL, data=params[i % len(params)], headers=headers, session=session)
            for i in xrange(no_requests))
for r in grequests.imap(reqs, size=200):
    ...

@29x10
Copy link

29x10 commented Jan 16, 2015

@rtdean fixed my problem

@cyberstip
Copy link

For posterity, just adding a session to the request seems to fix the issue (as @rtdean mentioned):

session = requests.Session()
reqs = (grequests.get(url, session=session) for url in urls)
results = grequests.map(reqs, size=50)

@bbbco
Copy link

bbbco commented Oct 5, 2015

It would be great if you could add this info to a FAQ section in the README

@skasturi
Copy link

skasturi commented Feb 4, 2017

We just faced this issue on our servers bringing down the whole service. It would be good to have it in the README. Thank you!

@amitsquare
Copy link

Please do mention using Session Object in README .As till you complete system shutdown , user won't be searching the issue & end up here on this issue thread .

@jontesek
Copy link

@amitsquare @skasturi @bbbco Feel free to create a PR for this :).

@spyoungtech
Copy link
Owner

To provide some further context, this is (or at least was) a problem with HTTPS libraries in general... See psf/requests#239 urllib3/urllib3#291

As far as I can tell, this is not a direct issue with grequests, so I'm going to close the issue. As far as updating the README... Personally, I'm not sure it's worthwhile adding to the currently concise README. I see no particular reason to document it than any other issue not specifically within grequest's domain that affects HTTP libraries in general. Though, if someone feels this is particularly useful for grequests users, please feel free to submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants