Blocking ctrl-c to os.popen() or os.system()
Sandeep Gupta
sandeep182 at hotmail.com
Sat Jan 4 23:29:10 EST 2003
Thanks, Chad. I was not clear in my previous message.
I am running on Linux, but the script I'm writing will also be using on
Win2k.
What I meant was I start the command and programatically wait for it to
finish. I wait for the command to finish by doing an f.read() on the stdout
returned from popen*(). While I am waiting for it to finish, if the user
types ctrl-c, the command receives the interrupt.
I also posted the question to the unix.shell newsgroup. Apparently, the
command subprocess is allowed to turn on the signal again, and that may be
happening in my case. If I turn off signal handling for SIGINT, and then
run the command "ls -R / | wc -l" (which takes a minute to run on my
system), then I am not able to interrupt the command. But if my command is
"cvs diff", cvs receives the ctrl-c.
Any other suggestions?
Thanks
"Chad Netzer" <cnetzer at mail.arc.nasa.gov> wrote in message
news:mailman.1041737589.4382.python-list at python.org...
> On Saturday 04 January 2003 14:00, Sandeep Gupta wrote:
> > I am executing a command from python via os.popen(). While the
> > command is executing, if I type ctrl-c, the command receives the
> > interrupt.
>
> What is your system? On Linux, with a trivial (and perhaps foolish)
example
> it seems that the Python interpreter does indeed get the ctrl-c:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~
> import os
> import time
>
> if __name__ == '__main__':
> cmd_str = "sleep 20"
> print 'Running "%s"' % cmd_str
> os.popen2( cmd_str )
>
> print "Python now sleeping for 20 seconds."
> try:
> time.sleep(20)
> except KeyboardInterrupt:
> print "ctrl-c was pressed"
> ~~~~~~~~~~~~~~~~~~~~~~~~
>
> $ python popen_test.py
> Running "sleep 20"
> Python now sleeping for 20 seconds.
> ctrl-c was pressed
>
>
> Note - I pressed ctrl-c to get that result. :)
>
> --
> Bay Area Python Interest Group - http://www.baypiggies.net/
>
> Chad Netzer
> cnetzer at mail.arc.nasa.gov
>
More information about the Python-list
mailing list