Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Terminating python script easily (http://www.velocityreviews.com/forums/t702524-terminating-python-script-easily.html)

Balban 10-22-2009 12:46 PM

Terminating python script easily
 
Hi,

I have a python build script that calls various commands, some using
os.system().

Often, if I want to terminate the script prematurely, I press ctrl-c,
but I have to do this many times before I can kill the script for
good. I was wondering is there a way that I define a signal handler
and kill the whole thing at once with a single ctrl-c? Perhaps I
should also call my other scripts with a method other than os.system
() as well?

Thank you,

Bahadir

Benjamin Kaplan 10-22-2009 12:59 PM

Re: Terminating python script easily
 
On Thu, Oct 22, 2009 at 8:46 AM, Balban <bilgehan.balban@gmail.com> wrote:
> Hi,
>
> I have a python build script that calls various commands, some using
> os.system().
>
> Often, if I want to terminate the script prematurely, I press ctrl-c,
> but I have to do this many times before I can kill the script for
> good. I was wondering is there a way that I define a signal handler
> and kill the whole thing at once with a single ctrl-c? Perhaps I
> should *also call my other scripts with a method other than os.system
> () as well?
>
> Thank you,
>
> Bahadir
> --

In Python, ctrl-C raises a KeyboarInterrupt. You can just catch that
and terminate everything in the except clause. If you're using 2.6,
subprocess.Popen has a terminate method you can use to quit the other
scripts if you use that instead of os.system.

Jean-Michel Pichavant 10-22-2009 01:01 PM

Re: Terminating python script easily
 
Balban wrote:
> Hi,
>
> I have a python build script that calls various commands, some using
> os.system().
>
> Often, if I want to terminate the script prematurely, I press ctrl-c,
> but I have to do this many times before I can kill the script for
> good. I was wondering is there a way that I define a signal handler
> and kill the whole thing at once with a single ctrl-c? Perhaps I
> should also call my other scripts with a method other than os.system
> () as well?
>
> Thank you,
>
> Bahadir
>

you may want to use subprocess instead of os.system.
On catching CTRL+C, you kill all the pid started with subprocess and
exit the script smoothly.

JM

Bahadir Balban 10-22-2009 01:03 PM

Re: Terminating python script easily
 
On Thu, Oct 22, 2009 at 4:01 PM, Jean-Michel Pichavant
<jeanmichel@sequans.com> wrote:
> Balban wrote:
>>
>> Hi,
>>
>> I have a python build script that calls various commands, some using
>> os.system().
>>
>> Often, if I want to terminate the script prematurely, I press ctrl-c,
>> but I have to do this many times before I can kill the script for
>> good. I was wondering is there a way that I define a signal handler
>> and kill the whole thing at once with a single ctrl-c? Perhaps I
>> should *also call my other scripts with a method other than os.system
>> () as well?
>>
>> Thank you,
>>
>> Bahadir
>>

>
> you may want to use subprocess instead of os.system.
> On catching CTRL+C, you kill all the pid started with subprocess and exit
> the script smoothly.
>
> JM
>


Hmm. OK, this is what I suspected I needed. So no explicit signal
catching is required I guess.

I will look into it, thanks.


Bahadir

Gabriel Genellina 10-23-2009 01:47 AM

Re: Terminating python script easily
 
En Thu, 22 Oct 2009 10:03:51 -0300, Bahadir Balban
<bilgehan.balban@gmail.com> escribió:
> On Thu, Oct 22, 2009 at 4:01 PM, Jean-Michel Pichavant
> <jeanmichel@sequans.com> wrote:
>> Balban wrote:
>>>
>>> Often, if I want to terminate the script prematurely, I press ctrl-c,
>>> but I have to do this many times before I can kill the script for
>>> good. I was wondering is there a way that I define a signal handler
>>> and kill the whole thing at once with a single ctrl-c?


>> you may want to use subprocess instead of os.system.
>> On catching CTRL+C, you kill all the pid started with subprocess and
>> exit
>> the script smoothly.

>
> Hmm. OK, this is what I suspected I needed. So no explicit signal
> catching is required I guess.


Note that KeyboardInterrupt (the exception generated by pressing ^C) may
be catched (sometimes inadvertedly) if the code uses a bare 'except'
clause, and then the program continues normally, effectively ignoring ^C.
The most generic 'except' clause should be, normally:
try: ...
except Exception: ...

--
Gabriel Genellina


Gabriel Genellina 10-23-2009 01:47 AM

Re: Terminating python script easily
 
En Thu, 22 Oct 2009 10:03:51 -0300, Bahadir Balban
<bilgehan.balban@gmail.com> escribió:
> On Thu, Oct 22, 2009 at 4:01 PM, Jean-Michel Pichavant
> <jeanmichel@sequans.com> wrote:
>> Balban wrote:
>>>
>>> Often, if I want to terminate the script prematurely, I press ctrl-c,
>>> but I have to do this many times before I can kill the script for
>>> good. I was wondering is there a way that I define a signal handler
>>> and kill the whole thing at once with a single ctrl-c?


>> you may want to use subprocess instead of os.system.
>> On catching CTRL+C, you kill all the pid started with subprocess and
>> exit
>> the script smoothly.

>
> Hmm. OK, this is what I suspected I needed. So no explicit signal
> catching is required I guess.


Note that KeyboardInterrupt (the exception generated by pressing ^C) may
be catched (sometimes inadvertedly) if the code uses a bare 'except'
clause, and then the program continues normally, effectively ignoring ^C.
The most generic 'except' clause should be, normally:
try: ...
except Exception: ...

--
Gabriel Genellina



All times are GMT. The time now is 01:15 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.