Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Interrupt Python C API

Reply
Thread Tools

Interrupt Python C API

 
 
googler.1.webmaster@spamgourmet.com
Guest
Posts: n/a
 
      04-15-2009
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.
 
Reply With Quote
 
 
 
 
Piet van Oostrum
Guest
Posts: n/a
 
      04-15-2009
>>>>> (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:
 
Reply With Quote
 
 
 
 
googler.1.webmaster@spamgourmet.com
Guest
Posts: n/a
 
      04-15-2009
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?
 
Reply With Quote
 
Piet van Oostrum
Guest
Posts: n/a
 
      04-16-2009
>>>>> (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:
 
Reply With Quote
 
googler.1.webmaster@spamgourmet.com
Guest
Posts: n/a
 
      04-16-2009
On 16 Apr., 11:08, Piet van Oostrum <p...@cs.uu.nl> wrote:
> >>>>> googler.1.webmas...@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 <p...@cs.uu.nl>
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: p...@vanoostrum.org


thx, i will check... merci
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interrupt python thread BlueBird Python 6 08-30-2008 08:35 AM
Calling the C API from Python and Python program from same C API -bidirectional Praveen, Tayal (IE10) Python 0 03-17-2005 06:33 AM
python/pyGtk : How to generate a keyboard interrupt? ahk Python 5 05-05-2004 10:41 AM
python's threading has no "interrupt"? Jane Austine Python 19 12-04-2003 07:16 AM
where can i find the core code of intel 8259A interrupt controller? Tony VHDL 0 11-13-2003 09:21 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57