Git Product home page Git Product logo

Comments (8)

cyberstip avatar cyberstip commented on August 25, 2024 4

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)

from grequests.

amitsquare avatar amitsquare commented on August 25, 2024 3

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 .

from grequests.

rtdean avatar rtdean commented on August 25, 2024 2

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):
    ...

from grequests.

29x10 avatar 29x10 commented on August 25, 2024

@rtdean fixed my problem

from grequests.

bbbco avatar bbbco commented on August 25, 2024

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

from grequests.

skasturi avatar skasturi commented on August 25, 2024

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!

from grequests.

jontesek avatar jontesek commented on August 25, 2024

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

from grequests.

spyoungtech avatar spyoungtech commented on August 25, 2024

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.

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.