requests.{get,post} timeout

dieter dieter at handshake.de
Fri Aug 25 02:52:01 EDT 2017


Jon Ribbens <jon+usenet at unequivocal.eu> writes:

> On 2017-08-24, Chris Angelico <rosuav at gmail.com> wrote:
>> On Thu, Aug 24, 2017 at 9:43 PM, Jon Ribbens <jon+usenet at unequivocal.eu> wrote:
>>> Where did you explain how it can be done without help? As far as I'm
>>> aware, you can't close the socket without help since you can't get
>>> access to it, and as you mentioned even if you were to do so the
>>> effect it would have on requests is completely undefined.
>>
>> In a single-threaded program, just hit Ctrl-C.
>
> By that, do you mean "kill the process"? That's obviously not
> a sensible answer in general, especially given we were including
> processes which have no terminal or user sitting there watching
> them.

In Python 2, there is "PyThreadState_SetAsyncExc" (defined in "pystate.c"),
documented as follows:

Asynchronously raise an exception in a thread.
   Requested by Just van Rossum and Alex Martelli.
   To prevent naive misuse, you must write your own extension
   to call this, or use ctypes.  Must be called with the GIL held.
   Returns the number of tstates modified (normally 1, but 0 if `id` didn't
   match any known thread id).  Can be called with exc=NULL to clear an
   existing async exception.  This raises no exceptions

int PyThreadState_SetAsyncExc(long id, PyObject *exc);

Together with a "read timeout", you can implement a total
timeout for your requests: you perform your request in a separate
thread; you set up a "connect/read timeout" (relatively small compared to the
total timeout; these timeouts ensure that the thread does not stall
inside the C runtime (where "PyThreadState_SetAsyncExc" has not effect));
you monitor the request runtime (maybe in a different thread)
and send it an exception when your global timeout is exceeded.


I do not know whether a similar API function is available for
Python 3 (but I suppose so).




More information about the Python-list mailing list