473,651 Members | 3,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Joining threads but allowing signals to main thread?

I have a python-2.5 program running under linux in which I spawn a
number of threads. The main thread does nothing while these subsidiary
threads are running, and after they all complete, the main thread will
then exit.

I know that I can manage this through the use of Thread.join(), but
when I do it as follows, the main thread doesn't respond to signals:

import sys, time, signal, threading

signaled = False

class Signaled(Except ion):
pass

def sighandler(sign um, frame):
global signaled
print 'aborted!'
signaled = True

def sigtest():
global signaled
if signaled:
raise Signaled

def myfunc(arg):
while True:
try:
sigtest()
# do something
except Signaled:
return

threads = []
for a in sys.argv[1:]:
t = threading.Threa d(myfunc, args=(a,))
threads.append( t)

# do some initialization

for s in (signal.SIGHUP, \
signal.SIGINT, \
signal.SIGQUIT, \
signal.SIGTERM) :
signal.signal(s , sighandler)

for t in threads:
t.start()

for t in threads:
t.join()

sys.exit(0)

However, if I get rid of the t.join() loop and replace the last three
executable lines of the program with these, the main thread responds to
signals just fine:

...

while threading.activ eCount() 1:
time.sleep(0.00 1)

sys.exit(0)

Is there any way to allow my program to respond to signals without
having to busy-wait in the main thread?

Thanks in advance.
--
Lloyd Zusman
lj*@asfast.com
God bless you.

Jan 11 '07 #1
0 1220

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

Similar topics

3
1744
by: Sebastian Meyer | last post by:
Hi Newsgroup, i have some problems with using threads and signals in one program. In my program i have three threads running, one for checking a directory at a specified interval to see if new data arrived, one waiting for work and the main thread. My problem is the following: I want to shutdown the program via a signal, so i used the signal module. I know, that signal handling mus be done by the main thread, the signal
2
2050
by: Holger Joukl | last post by:
Hi, migrating from good old python 1.5.2 to python 2.3, I have a problem running a program that features some threads which execute calls to an extension module. Problem is that all of a sudden, I cannot stop the program with a keyboard interrupt any more; the installed signal handler does not seem to receive the signal at all. This happens both if I rebuild this extension using python 2.3 headers/library and if I simply use the old...
23
2479
by: Antoon Pardon | last post by:
I have had a look at the signal module and the example and came to the conclusion that the example wont work if you try to do this in a thread. So is there a chance similar code will work in a thread? -- Antoon Pardon
4
16277
by: Gabriele Bartolini | last post by:
Hi, I am writing an application in C++ on Linux, using threads (this is my first experience with pthreads). The application itself is fine, it is just that I wanted to handle asynchronous signals correctly, in particular the SIGINT, preventing the application from stopping ungracefully. Indeed, I'd like to catch the signal, close everything in a consistent way and stop the application.
9
1871
by: Eric Sabine | last post by:
Can someone give me a practical example of why I would join threads? I am assuming that you would typically join a background thread with the UI thread and not a background to a background, but since I'm asking in the first place, assume that assumption to be very assuming. thanks, Eric
12
3157
by: Grant | last post by:
I am having great difficulty understanding this and any code samples I find online are kilometres long and complicated to understand...Please could someone give me a simple exampe of how to get a delegate passed to my worked class so that my main form can update its progress bar? I know it has something to do with begininvoke and endinvoke etc, but I just need a simple sample to understand the convention. Code from my main form:...
0
1002
by: Mitko Haralanov | last post by:
Hi everyone, First off, I know that this has been discussed before and I did a search but could not find anything that helped my situation. Here is the problem: I have a Python program that uses threads, forked processes, and signals and I can't seem to understand where the signals go. When the program starts, it creates a thread, which spins in select
1
11329
by: Chad J. Schroeder | last post by:
I've run into an "opportunity" in a Python application using threads and signals. Basically, there is a main process that spawns off a child thread that loops forever. In between iterations, the child thread sleeps for X seconds. All the while, the main thread loops forever doing its thing and also sleeps in between iterations (see the code at the end this message). Normally the application would be daemonized before the main
6
2380
by: geoffbache | last post by:
Hi all, I have a Python program (on UNIX) whose main job is to listen on a socket, for which I use the SocketServer module. However, I would also like it to be sensitive to signals received, which it isn't if it's listening on the socket. ("signals can only be received between atomic actions of the python interpreter", presumably - and control will not return to Python unless something appears on the socket). Does anyone have a tip of a...
0
8278
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
8807
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...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7299
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
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
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
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
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.