control-C not responding, short example program

David Bolen db3l at fitlinxx.com
Tue Jul 24 17:25:20 EDT 2001


ransen_spam_me_not at nemo.it (Owen F. Ransen) writes:

> If you put this in a module and test it:
> 
> def ForLoopTest (NumLoops) :
>     for Year in range (1,NumLoops) :
>         for Century in range (1,NumLoops) :
>             Months = Year*12
> 
> 
> you'll find that ForLoopTest (10000) will apparently
> halt 500Mhz Windows 98 machine. It simply does not
> respond to control-C. Is there a way of doing this
> or do I just have to live with it?

I can't reproduce this on an NT machine, so this may a Windows 98
issue more than a Python issue.  Under NT 4.0 I can break out of this
with Ctrl-C under Python 1.5.2, 2.0, 2.1.1 and 2.2a1.

If you want a somewhat ugly suggestion, what happens if you import the
msvcrt module and put a msvcrt.kbhit() call in the middle of your
loop?  I don't have a 98 machine to try it on, but if 98 is only
looking for the break during I/O (which is very DOS like, so maybe
95/98 inherit that behavior for console apps), that may give it the
opportunity it needs without actually performing I/O.

Since that would likely slow down the loop, you may want to put it in
one of the outer loops rather than in the innermost.

Another somewhat crufty approach would be to put your computations off
into a separate thread, and leave the main thread just sitting around
(perhaps waiting for an event to fire saying that the computation
thread is done).  Then the interrupt can probably be delivered to the
main thread which can cope with it.  If you define the computation
thread as a daemon you can just exit from the main thread even while
the computation thread is running.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list