Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > thread; interrupt_main() doesn't seem to work?

Reply
Thread Tools

thread; interrupt_main() doesn't seem to work?

 
 
Jerry Sievers
Guest
Posts: n/a
 
      11-21-2004
Fellow Pythonists; According to the docs

+-----------------+
|interrupt_main(|)|
+-----------------+

Raise a KeyboardInterrupt in the main thread. A subthread can use
this function to interrupt the main thread. New in version 2.3.

Therefore, I was expecting this short test prog to run and then finish
with the KeyboardInterrupt exception at the bottom in the pause
routine. It doesn't stop there but I can do a manual keyboard
interrupt and get the expected bactrace and program exit.

---

from signal import pause
from socket import socket
from thread import start_new_thread, interrupt_main
from time import sleep

def go():
print 'go'
while f.readline():
print 'line'
print 'empty'
interrupt_main()
print 'done'

s = socket()
s.connect(('localhost', 80))
f = s.makefile()
print 'connected'

start_new_thread(go, ())

sleep(2)
s.shutdown(2)
pause() # expecting to get KeyboardInterrupt exception here

---

connected
go
empty
done
....hangs here as if interrupt_main() does nothing.

Manual Control-C required to wake up from pause() and exit.

Traceback (most recent call last):
File "test.py", line 23, in ?
pause() # expecting to get KeyboardInterrupt exception here
KeyboardInterrupt

Python 2.3.3 on RedHat 7x.

Wondering what's wrong?

Thanks


--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      11-21-2004
Jerry Sievers wrote:

> Fellow Pythonists; According to the docs
>
> +-----------------+
> |interrupt_main(|)|
> +-----------------+
>
> Raise a KeyboardInterrupt in the main thread. A subthread can use
> this function to interrupt the main thread. New in version 2.3.
>
> Therefore, I was expecting this short test prog to run and then finish
> with the KeyboardInterrupt exception at the bottom in the pause
> routine. It doesn't stop there but I can do a manual keyboard
> interrupt and get the expected bactrace and program exit.
>
> ---
>
> from signal import pause
> from socket import socket
> from thread import start_new_thread, interrupt_main
> from time import sleep
>
> def go():
> print 'go'
> while f.readline():
> print 'line'
> print 'empty'
> interrupt_main()
> print 'done'
>
> s = socket()
> s.connect(('localhost', 80))
> f = s.makefile()
> print 'connected'
>
> start_new_thread(go, ())
>
> sleep(2)
> s.shutdown(2)
> pause() # expecting to get KeyboardInterrupt exception here
>
> ---
>
> connected
> go
> empty
> done
> ....hangs here as if interrupt_main() does nothing.
>
> Manual Control-C required to wake up from pause() and exit.
>
> Traceback (most recent call last):
> File "test.py", line 23, in ?
> pause() # expecting to get KeyboardInterrupt exception here
> KeyboardInterrupt
>
> Python 2.3.3 on RedHat 7x.
>
> Wondering what's wrong?
>
> Thanks
>
>

Perhaps the main thread has to actually be running to respond to the
raised exception. Did you try a shortish sleep() instead of the pause?
That would at least test this theory.

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
 
Reply With Quote
 
 
 
 
Jerry Sievers
Guest
Posts: n/a
 
      11-21-2004
Steve Holden <(E-Mail Removed)> writes:

> Jerry Sievers wrote:
>
> > Fellow Pythonists; According to the docs
> >
> > +-----------------+
> > |interrupt_main(|)|
> > +-----------------+
> >
> > Raise a KeyboardInterrupt in the main thread. A subthread can use
> > this function to interrupt the main thread. New in version 2.3.
> >
> > Therefore, I was expecting this short test prog to run and then finish
> > with the KeyboardInterrupt exception at the bottom in the pause
> > routine. It doesn't stop there but I can do a manual keyboard
> > interrupt and get the expected bactrace and program exit.
> >
> > ---
> >
> > from signal import pause
> > from socket import socket
> > from thread import start_new_thread, interrupt_main
> > from time import sleep
> >
> > def go():
> > print 'go'
> > while f.readline():
> > print 'line'
> > print 'empty'
> > interrupt_main()
> > print 'done'
> >
> > s = socket()
> > s.connect(('localhost', 80))
> > f = s.makefile()
> > print 'connected'
> >
> > start_new_thread(go, ())
> >
> > sleep(2)
> > s.shutdown(2)
> > pause() # expecting to get KeyboardInterrupt exception here
> >
> > ---
> >
> > connected
> > go
> > empty
> > done
> > ....hangs here as if interrupt_main() does nothing.
> >
> > Manual Control-C required to wake up from pause() and exit.
> >
> > Traceback (most recent call last):
> > File "test.py", line 23, in ?
> > pause() # expecting to get KeyboardInterrupt exception here
> > KeyboardInterrupt
> >
> > Python 2.3.3 on RedHat 7x.
> >
> > Wondering what's wrong?
> >
> > Thanks
> >
> >

> Perhaps the main thread has to actually be running to respond to the
> raised exception. Did you try a shortish sleep() instead of the pause?
> That would at least test this theory.


Sure did. Tested with the main prog blocked on raw_input() and also
sleeping(someNum)...

But you did hit on the problem when you said 'running'.

I kept testing and found it to work in the following test case;

While True:
sleep(1)
# the exception gets noticed here
doSomething()

This is a little curious since a real KeyboardInterrupt does get
noticed and processed immediatly whereas the one thrown from the
interrupt_main() routine doesn't get noticed until "between"
instructions.

In other words, I can interrupt a pause() at any time by hitting
Control-C but not by calling interrupt_main().

Thank you


--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
 
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
Windows ME doesn't seem to send out DHCP request Alexander Skwar Wireless Networking 0 10-02-2005 11:42 AM
laptop/wireless doesn't seem to pick up wireless networks? Bruce Wireless Networking 9 09-24-2005 01:23 AM
router doesn't seem to work =?Utf-8?B?ZA==?= Wireless Networking 2 06-12-2005 06:45 PM
I have two situations I cant seem to fix on my own! =?Utf-8?B?bW9tbXlraXNzZXM=?= Microsoft Certification 0 05-29-2004 06:31 PM
Error downloading page, some pages work great but cant seem to get this one Jack Schafer Perl 1 04-23-2004 08:32 AM



Advertisments