cross platform alternative for signal.SIGALRM?

Cameron Simpson cs at zip.com.au
Thu Nov 12 00:20:46 EST 2015


On 11Nov2015 16:16, Ulli Horlacher <framstag at rus.uni-stuttgart.de> wrote:
>I am rewriting a Perl program into Python (2.7).
>It must run on Linux and Windows.
>With Linux I have no problems, but Windows... :-(
>
>The current show stopper is signal.SIGALRM which is not available on
>Windows:
>
>  File "fexit.py", line 674, in formdata_post
>    signal.signal(signal.SIGALRM,timeout_handler)
>  AttributeError: 'module' object has no attribute 'SIGALRM'
>
>  https://docs.python.org/2/library/signal.html
>
>  signal.alarm(time) (...) Availability: Unix.
>
>Perl for Windows has had SIGALRM support (or some kind of emulation).
>
>Ok, I have to redesign this part of my code:
>
>  def timeout_handler(sig,frame):
>    raise ValueError("timeout!")
>
>  signal.signal(signal.SIGALRM,timeout_handler)
>
>  while True:
>    chunk = fileo.read(bs)
>    sock.sendall(chunk)
>    (...)
>
>What is the best practise for a cross platform timeout handler?

I suggest you look at the socket.settimeout function. Avoid SIGALRM altogether.  
Then (untested):

  import socket
  ...
  socket.settimeout(timeout_in_seconds)
  ...
  while True:
    ...
    chunk = fileo.read(bs)
    try:
      sock.sendall(chunk)
    except socket.timeout as e:
      ... complain about timeout, reciting "e" in the message ...

Cheers,
Cameron Simpson <cs at zip.com.au>

I think you're confusing "recognizing" and "understanding" with "caring".
The net is cruel, sometimes, but always fair.
        - Rick Gordon <rickg at crl.com>



More information about the Python-list mailing list