requests.{get,post} timeout

Chris Angelico rosuav at gmail.com
Wed Aug 23 13:29:08 EDT 2017


On Thu, Aug 24, 2017 at 2:59 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Chris Angelico <rosuav at gmail.com>:
>
>> But also, this honestly isn't as big an issue as you might think. If
>> the user thinks a program has been running for too long, s/he can hit
>> Ctrl-C. Voila! Signal is sent, which aborts a socket read,
>
> Well, no, it doesn't. First run:
>
> ========================================================================
> nc -l -p 12345
> ========================================================================
>
> in one window. Then, execute this program in another one:
>
> ========================================================================
> import threading, socket
>
> def f():
>     s = socket.socket()
>     try:
>         s.connect(("localhost4", 12345))
>         s.recv(1000)
>     finally:
>         s.close()
>
> t = threading.Thread(target=f)
> t.start()
> t.join()
> ========================================================================
>
> After you hit Ctrl-C once (under Linux), you get this trace:

[chomp]

What I said was that you don't need threading or alarms because most
of the time you can let the user use SIGINT. And without the (utterly
totally useless) threading that you have here, it works flawlessly:
Ctrl-C instantly breaks the recv call.

All you've demonstrated is that Ctrl-C halts a long-running request
*in the main thread*, which in this case is your join(). And when I
tested it interactively, it left the subthread running and halted the
join. The reason you see the "hit Ctrl-C again" phenomenon is that the
program wants to join all threads on termination. Solution 1: Keep the
program running but halt the request. Solution 2: Daemonize the
thread. Just run "t.daemon = True" before starting the thread, and the
program terminates cleanly after one Ctrl-C. I'd prefer solution 1,
myself, but you can take your pick.

ChrisA



More information about the Python-list mailing list