How can I cancel an in-flight Requests.Session.Get request?
python, python-requests, sockets
Solution
After looking through everything I could think of, it looks like `requests` has no way to cancel an in-flight request without getting all the way down to the socket level. And, as the OP pointed out, even that doesn't help directly on Windows, unless you actually get into the underlying select-or-pool loop instead of just closing the socket. (And if you didn't care about Windows, you could more easily cancel the reads just by `signal`ing the thread…) So, that's pretty much out.
One alternative is to just kill the threads, or let the OS do it for you. If the thread does other work too… well, you can always split that off onto different threads. But that could be a major, and unwarranted, rewrite of your code.
So, it sounds like you need to give up `requests` and use something else.
The cURL library definitely has the ability to cancel requests. There's an official set of bindings called `pycurl`, but it's much lower-level than `requests`, so you may want to look over the 69105 higher-level wrappers you can find by searching at PyPI. Some of them are not very complete/stable/up-to-date, so you probably need to do a bit of playing around to find the right one for you…
Problem
So I have a helper thread that listens for info from a server using https. I'm using Python's Requests.Session and calling get() with no timeout because we do want to wait for some response. However, at shutdown, I want to nicely end all my helper threads. I'm not sure how to kill the get request which the thread is blocking on. On *nix environments closing the underlying socket for the session will end up throwing up an exception. So thats a way to do this; close socket, catch exception, be happy. On Windows this doesn't seem to work tho. What is the right way to kill/end the session and the get request so that my helper thread can die? Clarification: I'm using Python's Requests.Session.get().