I found this question using Google, and used Spike Gronim's answer to come up with:
from gevent import monkey monkey.patch_all() import gevent import requests def post(*args, **kwargs): if 'stop_event' in kwargs: stop_event = kwargs['stop_event'] del kwargs['stop_event'] else: stop_event = None req = gevent.spawn(requests.post, *args, **kwargs) while req.value is None: req.join(timeout=0.1) if stop_event and stop_event.is_set(): req.kill() break return req.value
I thought it could be useful for other people.
It works the same as a regular request.post, but accepts the optional keyword argument 'stop_event'. This is threading.Event. The request will terminate if stop_event is set.
Use with caution, because if it does not wait for a connection or connection, it can block the GIL (as mentioned). It (gevent) seems compatible with streams these days (via a monkey patch).
source share