Interrupt Python C API

Discussion in 'Python' started by googler.1.webmaster@spamgourmet.com, Apr 15, 2009.

  1. Guest

    Hi,

    I just have a design problem and don't know how to solve it. I call a
    function which
    executes a simple "PyRun_String(...)" command.

    imagine the script while 1: pass is executed so the app would hang. Is
    there any chance
    to break out this PyRun_String-function? I just searched the forums
    for that stuff
    but these information are very rare.

    Thanks for any suggestions.

    Bye.
    , Apr 15, 2009
    #1
    1. Advertising

  2. >>>>> (g) wrote:

    >g> Hi,
    >g> I just have a design problem and don't know how to solve it. I call a
    >g> function which
    >g> executes a simple "PyRun_String(...)" command.


    >g> imagine the script while 1: pass is executed so the app would hang. Is
    >g> there any chance
    >g> to break out this PyRun_String-function? I just searched the forums
    >g> for that stuff
    >g> but these information are very rare.


    >g> Thanks for any suggestions.


    I think PyRun_String is similar to exec. If you are on a Unix system you
    can use the alarm signal. Here is a pure Python example, but I suspect
    you can translate this to C in a straightforward manner.

    import signal

    class AlarmError(Exception):
    pass

    def handler(signum, frame):
    raise AlarmError, "command lasts too long"

    signal.signal(signal.SIGALRM, handler)

    def execute(command, timeout):
    signal.alarm(timeout);
    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 <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
    Piet van Oostrum, Apr 15, 2009
    #2
    1. Advertising

  3. Guest

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

    Is there a chance for windows, too?
    , Apr 15, 2009
    #3
  4. >>>>> (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 <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
    Piet van Oostrum, Apr 16, 2009
    #4
  5. Guest

    On 16 Apr., 11:08, Piet van Oostrum <> wrote:
    > >>>>> (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 <>
    > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
    > Private email:


    thx, i will check... :) merci
    , Apr 16, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony
    Replies:
    0
    Views:
    1,034
  2. Jane Austine

    python's threading has no "interrupt"?

    Jane Austine, Nov 28, 2003, in forum: Python
    Replies:
    19
    Views:
    2,535
    Anand Pillai
    Dec 4, 2003
  3. ahk
    Replies:
    5
    Views:
    2,444
    Peter Hansen
    May 5, 2004
  4. Praveen, Tayal (IE10)
    Replies:
    0
    Views:
    363
    Praveen, Tayal (IE10)
    Mar 17, 2005
  5. BlueBird

    Interrupt python thread

    BlueBird, Aug 24, 2008, in forum: Python
    Replies:
    6
    Views:
    819
    Dieter Maurer
    Aug 30, 2008
Loading...

Share This Page