1

Here is how I am making an HTTP Post request from a worker thread.

while some_conditon:
   # get data from the queue.
   resp = requests.post('www.test.com', data=mydata, timeout=30)

In the above scenario, when I want to exit the application, and say the requests.post method has already executed, I will have to wait for 30 sec in the worst case. Is there a way to abort the request and exit immediately?

I know I can make the worker thread as a daemon thread and simply exit the main thread but that leads to abrupt killing of the worker thread which leads to unexpected exception.

One way to handle this would be to use futures and periodically check if the future is done, something similar to this How do you kill Futures once they have started? but I wanted to know if there is any other way to do it.

rdp
  • 2,072
  • 4
  • 31
  • 55
  • According to [the docs](http://docs.python-requests.org/en/master/user/advanced/#blocking-or-non-blocking), there's no way to do this directly with `requests`, but different ways of doing this with different libs that asynchronize` requests` in different ways. – abarnert Mar 19 '18 at 00:04
  • Alternatively, I'm pretty sure that, even though it isn't actually _designed_ to work this way, if you just reach into a requests session, grab its socket, and close it, requests will get an error immediately and probably handle it safely. – abarnert Mar 19 '18 at 00:07
  • Or… do you have to use `requests`? With another library like `aiohttp`, or one of the low-level `curl` wrappers where you have to manage the sockets and poll manually, canceling is pretty easy. – abarnert Mar 19 '18 at 00:07
  • Thanks @abarnert, I am using `requests` currently, so for minimum changes in the code, it would be better for me to stick with the `requests` lib. But like you mentioned, the `requests` lib does not provide a direct way. At this point, I am open to explore other libraries too. – rdp Mar 19 '18 at 00:21
  • How do you exit your application? – Sraw Mar 19 '18 at 01:44
  • @Sraw When I receive the exit signal, I set `some_condition` to False, which will end the loop. – rdp Mar 19 '18 at 13:48

0 Answers0