Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

How to Force exiting from program/script

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


magobin at gmail

Jul 15, 2009, 2:26 PM

Post #1 of 3 (134 views)
Permalink
How to Force exiting from program/script

hi at all,
I have made a script with a while loop and I want that after 30
seconds the program stop and exit . But the code like this doesn't
run:
In the Console I can see work so that function is correctly called...

#Function to exit
def exit():
print "work"
raise SystemExit()
t = threading.Timer(30.0, exit)
t.start()

# Loop
while True:
...many lines....

--
http://mail.python.org/mailman/listinfo/python-list


deets at nospam

Jul 15, 2009, 2:39 PM

Post #2 of 3 (122 views)
Permalink
Re: How to Force exiting from program/script [In reply to]

Alex schrieb:
> hi at all,
> I have made a script with a while loop and I want that after 30
> seconds the program stop and exit . But the code like this doesn't
> run:
> In the Console I can see work so that function is correctly called...
>
> #Function to exit
> def exit():
> print "work"
> raise SystemExit()
> t = threading.Timer(30.0, exit)
> t.start()
>
> # Loop
> while True:
> ...many lines....
>

This works for me:

import threading
import time

def work():
while True:
pass



t = threading.Thread(target=work)
t.setDaemon(True)
t.start()

time.sleep(10)
raise SystemExit


The trick is to raise the SystemExit in the main-thread.

Diez
--
http://mail.python.org/mailman/listinfo/python-list


piet at cs

Jul 16, 2009, 1:48 AM

Post #3 of 3 (108 views)
Permalink
Re: How to Force exiting from program/script [In reply to]

>>>>> Alex <magobin[at]gmail.com> (A) a écrit:

>A> hi at all,
>A> I have made a script with a while loop and I want that after 30
>A> seconds the program stop and exit . But the code like this doesn't
>A> run:
>A> In the Console I can see work so that function is correctly called...

>A> #Function to exit
>A> def exit():
>A> print "work"
>A> raise SystemExit()
>A> t = threading.Timer(30.0, exit)
>A> t.start()

>A> # Loop
>A> while True:
>A> ...many lines....

This code gives you a bit more control as it doesn't just force a system
exit, but allows you to continue some other work after aborting the
loop:

import signal, os
from threading import Timer

signalcode = signal.SIGALRM
class AlarmError(Exception):
pass

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

signal.signal(signalcode, handler)

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

def execute(function, timeout):
Timer(timeout, interrupt).start()
try:
function()
except AlarmError, e:
print e
print 'Execution aborted'

def long_function():
while True:
pass

print "The everlasting command"
execute(long_function, 10)
print "The End"

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

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.