Interrupt Python C API

Piet van Oostrum piet at cs.uu.nl
Thu Apr 16 05:08:18 EDT 2009


>>>>> googler.1.webmaster at spamgourmet.com (g1w) wrote:

>g1w> hi, yes, thats true, Alan Touring told us, so it would be nice to let
>g1w> the user abort it.

>g1w> Is there a chance for windows, too?

I don't know. I have no access to Python on Windows. Maybe there is
setitimer support on Windows. Or maybe you can use the threading.Timer
object, like this:

import signal, os
import threading

signalcode = signal.SIGALRM # on Windows, choose one that exists there.

class AlarmError(Exception):
    pass

def interrupt():
    os.kill(os.getpid(), signalcode)

def execute(command, timeout):
    threading.Timer(timeout, interrupt).start()
    try:
        exec(command)
    except AlarmError, e:
        print e
        print 'Aborted "%s"' % (command,)
    print "continue work"

print "The everlasting command"
execute("while 1: pass", 10)
print "The End"

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list