473,387 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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/
Jul 18 '05 #1
2 6790
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
Jul 18 '05 #2
Steve Holden <st***@holdenweb.com> 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/
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: duncan | last post by:
I am using SQL Server 2000 This doesn't work :- DECLARE @topnum AS INT SET @topnum = 2 SELECT TOP @topnum * FROM tblMyTable
4
by: Developwebsites | last post by:
#ifndef PERSON_H #define PERSON_H #include<iostream> #include<iomanip> #include<string> class Person { protected: char name;
0
by: Kueishiong Tu | last post by:
I have a window form application. It requires to retrieve data from a web site. Since the web request is very time consuming, so I create a work thread to do the job. How does the window main form...
3
by: Thomas Richter | last post by:
Hi, a thread I start with: threadQuoteRunner = new Thread(new ThreadStart(_quoteHandler.QuoteRunner)); threadQuoteRunner.Start(); but if I try to stop him, it doesn't work:...
1
by: JezB | last post by:
I have some javascript to restore the scroll position of a DIV over postbacks. I've tested it on all major browsers and the only one it doesn't work on is Opera. Could someone tell me why, and what...
1
by: Lopamudra | last post by:
Hi, I have implemented a HTTPHandler named ThumbnailGenerator.ashx to generate thumbnails for images. I have referenced this within my aspx file in the ImageUrl property of the Img control. When I...
9
by: xmlhelp | last post by:
stuff.XSL: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="uid"/> <xsl:template match="/"> parameter...
10
by: rockdc1981 | last post by:
Private Sub SupplierSKUCode_AfterUpdate() If Nz(DLookup("SupplierSKUCode", "tblProduct", "SupplierSKUCode='" & _ Me.SupplierSKUCode & "'"), "zzzz") <> "zzzz" Then MsgBox...
2
by: Mark Cooney | last post by:
Hi Why doesnt this work? <% CatID = dsMain.SelectParameters("fkCatID").ToString%> or <% CatID = Eval("fkCatID") %> Anyone have a solution?
4
by: supriyamk | last post by:
Hi, i am trying to copy files of a certain type into a different directory using perl, The copy function doesnt seem to work. my @dir_list; my $sub_dir; my $file_name1; ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.