Comments (8)
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.
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.
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.
@rtdean fixed my problem
from grequests.
It would be great if you could add this info to a FAQ section in the README
from grequests.
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.
@amitsquare @skasturi @bbbco Feel free to create a PR for this :).
from grequests.
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)
- limit number of requests per second. HOT 1
- why size is no affect in map? HOT 1
- Add index in imap HOT 3
- Params of get/post functions HOT 3
- Import fails HOT 1
- Grequests warning and crash HOT 3
- Docker crash HOT 2
- Request for image HOT 2
- Append wait time or rate for every thread due to server limits HOT 1
- Python 3.6 the fastest environment?
- Python 3.6 the fastest environment? HOT 2
- gtimeout does not works with imap_enumerated HOT 1
- Flag to print the request being executed HOT 2
- How to ignore SSL cert verification failed in grequests? HOT 2
- grequests.map() is slow when passing requests with the data parameter HOT 2
- nose should be in `dev_requirements.txt`
- Process finished with exit code 132 (interrupted by signal 4: SIGILL) HOT 1
- how to parse single response when setting stream=True in map function?
- "Too many open files" error + memory leak due to dangling TCP connections HOT 1
- feature: Get timing metrics HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grequests.