473,795 Members | 3,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Fellow Pythonists; According to the docs

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

Raise a KeyboardInterru pt 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 KeyboardInterru pt 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_threa d, 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(('loc alhost', 80))
f = s.makefile()
print 'connected'

start_new_threa d(go, ())

sleep(2)
s.shutdown(2)
pause() # expecting to get KeyboardInterru pt 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 KeyboardInterru pt exception here
KeyboardInterru pt

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 6826
Jerry Sievers wrote:
Fellow Pythonists; According to the docs

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

Raise a KeyboardInterru pt 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 KeyboardInterru pt 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_threa d, 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(('loc alhost', 80))
f = s.makefile()
print 'connected'

start_new_threa d(go, ())

sleep(2)
s.shutdown(2)
pause() # expecting to get KeyboardInterru pt 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 KeyboardInterru pt exception here
KeyboardInterru pt

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***@holdenwe b.com> writes:
Jerry Sievers wrote:
Fellow Pythonists; According to the docs

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

Raise a KeyboardInterru pt 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 KeyboardInterru pt 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_threa d, 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(('loc alhost', 80))
f = s.makefile()
print 'connected'

start_new_threa d(go, ())

sleep(2)
s.shutdown(2)
pause() # expecting to get KeyboardInterru pt 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 KeyboardInterru pt exception here
KeyboardInterru pt

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(someNu m)...

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 KeyboardInterru pt 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
3258
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
1639
by: Developwebsites | last post by:
#ifndef PERSON_H #define PERSON_H #include<iostream> #include<iomanip> #include<string> class Person { protected: char name;
0
1177
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 (which is the main thread) know when the work thread has finished the job so that it can display the data received on the form? I do not want to use a global variable since if the main form is continuously checking for the global variable, it...
3
3044
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: threadQuoteRunner.Abort(); In the constructor of QuoteRunner is an while (true) loop. In the debugger I can stop the programm. But if the program is started
1
1352
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 I can do to (a) test that it's Opera that's running and (b) how to get and set the scroll position - scrollTop does not seem to work. Here's the code : // scrolls the "what" element given to it (the DIV scroll area) // call from body onload...
1
1857
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 debug the ashx page by setting it as the start page, the handler executes ( and also writes a debug line on the Application event log). But when I debug the aspx page, it sets the ImageUrl property and executes properly, but doesnt seem to...
9
2530
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 UID=<xsl:value-of select="$uid"/> <table border="1"> <tr> <xsl:for-each select="row/value=20]">
10
2370
by: rockdc1981 | last post by:
Private Sub SupplierSKUCode_AfterUpdate() If Nz(DLookup("SupplierSKUCode", "tblProduct", "SupplierSKUCode='" & _ Me.SupplierSKUCode & "'"), "zzzz") <> "zzzz" Then MsgBox "SupplierSKUCode Already Exist.", vbCritical If MsgBox("Duplicate Record." _ & vbCrLf & vbCrLf & "Do you want to have Duplicate Record?" _ , vbYesNo, "Changes Made...") = vbYes Then Me.ProductDescription.SetFocus Else ...
2
1465
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
1663
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; my $file_name2; my @file_list_gel; my @file_list_res;
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.