Comments (7)
interesting. that's not a use-case i've come across myself so hadn't though about addressing.
is there a specific reason you're creating a bunch of sessions and not a single long-lived FuturesSession to be shared across time. unless each one needs to be its own distinct session (cookies etc.) then you would probably be better off creating a single FuturesSession with a larger number of max_workers and just let it live for the life of the script.
i'm not completely opposed to FuturesSession implementing the context manager protocol, just want to make sure that it's needed first.
from requests-futures.
I think cleaning up the thread pool resources is akin to calling .close()
on file objects when we're done with them. And open()
follows the context manager pattern to give you a convenient wrapper that automatically calls the .close()
, so that's where I got the idea from.
I agree that for my script it's better to use just a single FuturesSession
, but I feel it's good practice to clean up resources regardless.
from requests-futures.
feel free to pr that change to FuturesSession and ideally provide an example in the README. i assume the example should catch and use the session.
with FuturesSession(max_workers=2) as session:
session...
ideally there'd be some sort of unit testing of the functionality. perhaps there's a way to tell if the executor has been shutdown correctly.
from requests-futures.
+1. Seems useful when chunking large numbers of requests (I'm doing 5000 per FuturesSession)
from requests-futures.
happy to accept patches w/tests. otherwise i'll try and get to it in an upcoming weekend.
from requests-futures.
Heh I was dragging my feet on the PR because of the difficulty of writing a proper unit test. I've investigated the concurrent.futures
module, and there's only 2 ways I can think of to determine if the executor has been shutdown:
- Inspect
executor._shutdown
which is a private field onThreadPoolExecutor
(https://hg.python.org/cpython/file/3.2/Lib/concurrent/futures/thread.py#l125). Feels really icky to rely on private API. - Rely on the documented fact that a
RuntimeError
will be raised if we try to use theThreadPoolExectuor
again: (https://docs.python.org/3.2/library/concurrent.futures.html#concurrent.futures.Executor.shutdown): "Calls to Executor.submit() and Executor.map() made after shutdown will raise RuntimeError."
Option 2 sounds slightly more proper but still icky in that it's not directly asserting what we intended, but a side effect.
Lemme know which option sounds better and I can try to do a PR with it.
from requests-futures.
another option might be to monkey patch executor.shutdown in the unit test and replace it with something that sets a flag and calls the original.
or slightly cleaner, inherit from FuturesSession and override exit and set a flag that can be checked there.
definitely a tough thing to test, that it shut down as designed. i guess the most important part to test is that the object functions in the with context correctly. that it calls exit is nice to test, but not critical.
from requests-futures.
Related Issues (20)
- OSError Issues when used within celery task (gevent) HOT 2
- Adding attribute to response via hook (as seen in examples) fails HOT 1
- Cannot switch to a different thread HOT 3
- Possibility to accept old SSL versions? HOT 1
- Problem with data field in header request HOT 1
- Making a future request and letting callback work with result - some are missing HOT 2
- Error When there is a large parallel Requests go. HOT 2
- Provide wheels on PyPI HOT 1
- How do we pass arguments to the hooks? HOT 3
- AttributeError: 'Future' object has no attribute 'status_code' HOT 1
- 1.0.0: missing git tag HOT 5
- Response for batched requests HOT 6
- Add type annotations HOT 4
- Not showing as updated on PyPI HOT 3
- On exception it returns xx (Caused By None), whereas the requests module returns xx (Caused By xx) HOT 4
- The elapsed time hook example does not work HOT 3
- 1.0.0: pytest is failing in two units HOT 6
- Use pytest-httpbin? HOT 8
- Attribute set via response hooks does not work with ProcessPoolExecutor HOT 4
- RFE: preserve contextvars when performing the request 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 requests-futures.