473,395 Members | 1,972 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,395 software developers and data experts.

Threads and signals

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
process starts up. Hence, during a system shutdown or stoppage of the daemon,
it's
desired that the daemon catch a few signals (TERM/INT) and perform a few cleanup
routines. According to the Python docs, only the main thread will receive
signals. The
problem I have, on FreeBSD systems, is that the sleep function in the child gets
interrupted
and the signal never gets handled until the main thread's sleep concludes. It
works as
expected on a Linux box (main thrd's sleep is interrupted). Sample output from
multiple
systems is directly below.

Just looking for insight from others in the know.

Thanks,
Chad
Linux 2.6.17
------------------------------------------------------------------------
test_bed$ python2.5 ./thrd_test.py
Starting up
MainThread.run(): 14332
MainThread before sleep(15)
ChildThread.run(): 14332
ChildThread before sleep(10)
Received signal 2 <-- Interrupted here (Ctrl-C), correctly
flag: True interrupts sleep in main thread
flag: False
MainThread after sleep(15)
Shutting down

test_bed$ python2.4 ./thrd_test.py
Starting up
MainThread.run(): 14338
MainThread before sleep(15)
ChildThread.run(): 14338
ChildThread before sleep(10)
Received signal 2 <-- Interrupted here (Ctrl-C), correctly
flag: True interrupts sleep in main thread
flag: False
MainThread after sleep(15)
Shutting down
test_bed$

FreeBSD 4.11, 6.1, and 6.2
------------------------------------------------------------------------
bash-2.05b# python2.5 ./thrd_test.py
Starting up
MainThread.run(): 65930
ChildThread.run(): 65930
ChildThread before sleep(10)
MainThread before sleep(15)
^CChildThread after sleep(10) <-- Interrupted here (Ctrl-C), but
ChildThread before sleep(10) interrupts the child thrd's sleep
ChildThread after sleep(10)
ChildThread before sleep(10)
Received signal 2 <-- Main sleep concludes and then
the
flag: True signal gets handled
flag: False
MainThread after sleep(15)
Shutting down
bash-2.05b#
#--- CODE BEGIN ---#

#!/usr/bin/env python

import os
import sys
import time
import signal
import threading

def sigHandle(signo, stkframe):
print "Received signal ", signo
print "flag: ", mthrd.flag.isSet()
mthrd.flag.clear()
print "flag: ", mthrd.flag.isSet()

class ChildThread(threading.Thread):

def __init__(self):
threading.Thread.__init__(self)

def run(self):
print "ChildThread.run(): ", os.getpid()
while (True):
print "ChildThread before sleep(10)"
time.sleep(10)
print "ChildThread after sleep(10)"

class MainThread(object):

def __init__(self):
self.flag = threading.Event()
self.cthrd = ChildThread()
self.cthrd.setDaemon(True)

def run(self):
print "MainThread.run(): ", os.getpid()
self.flag.wait()
self.cthrd.start()
while (self.flag.isSet()):
print "MainThread before sleep(15)"
time.sleep(15)
print "MainThread after sleep(15)"
return # or cleanup routine

if __name__ == "__main__":

# Normally, the process is daemonized first. That's been
# left out for testing purposes.

signal.signal(signal.SIGINT, sigHandle)
signal.signal(signal.SIGQUIT, sigHandle)
signal.signal(signal.SIGTERM, sigHandle)

mthrd = MainThread()
print "Starting up"
mthrd.flag.set()
mthrd.run()
print "Shutting down"

sys.exit(0)

#--- CODE END ---#

Nov 15 '06 #1
1 11310
By writing a little C module I fixed the problem. By simply calling
the function in the child thrd's run method, things work as expected.

This is all the function (blockall) does:

sigset_t omask, nmask;
sigfillset(&nmask);
sigprocmask(SIG_SETMASK, &nmask, NULL);

bash-2.05b# python2.5 ./thrd_test.py
Starting up
MainThread.run(): 71799
ChildThread.run(): 71799
ChildThread before sleep(10)
MainThread before sleep(15)
^CReceived signal 2
flag: True
flag: False
MainThread after sleep(15)
Shutting down
bash-2.05b#

Chad
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 process starts up. Hence, during a
system shutdown or stoppage of the daemon, it's desired that the
daemon catch a few signals (TERM/INT) and perform a few cleanup
routines. According to the Python docs, only the main thread will receive
signals. The problem I have, on FreeBSD systems, is that the sleep
function in the child gets interrupted and the signal never gets handled
until the main thread's sleep concludes. It works as expected on a Linux
box (main thrd's sleep is interrupted). Sample output from multiple systems
is directly below.

Just looking for insight from others in the know.

Thanks,
Chad
Linux 2.6.17
------------------------------------------------------------------------
test_bed$ python2.5 ./thrd_test.py
Starting up
MainThread.run(): 14332
MainThread before sleep(15)
ChildThread.run(): 14332
ChildThread before sleep(10)
Received signal 2 <-- Interrupted here (Ctrl-C), correctly
flag: True interrupts sleep in main thread
flag: False
MainThread after sleep(15)
Shutting down

test_bed$ python2.4 ./thrd_test.py
Starting up
MainThread.run(): 14338
MainThread before sleep(15)
ChildThread.run(): 14338
ChildThread before sleep(10)
Received signal 2 <-- Interrupted here (Ctrl-C), correctly
flag: True interrupts sleep in main thread
flag: False
MainThread after sleep(15)
Shutting down
test_bed$

FreeBSD 4.11, 6.1, and 6.2
------------------------------------------------------------------------
bash-2.05b# python2.5 ./thrd_test.py
Starting up
MainThread.run(): 65930
ChildThread.run(): 65930
ChildThread before sleep(10)
MainThread before sleep(15)
^CChildThread after sleep(10) <-- Interrupted here (Ctrl-C), but
ChildThread before sleep(10) interrupts the child thrd's sleep
ChildThread after sleep(10)
ChildThread before sleep(10)
Received signal 2 <-- Main sleep concludes and then
flag: True the signal gets handled
flag: False
MainThread after sleep(15)
Shutting down
bash-2.05b#
#--- CODE BEGIN ---#

#!/usr/bin/env python

import os
import sys
import time
import signal
import threading

def sigHandle(signo, stkframe):
print "Received signal ", signo
print "flag: ", mthrd.flag.isSet()
mthrd.flag.clear()
print "flag: ", mthrd.flag.isSet()

class ChildThread(threading.Thread):

def __init__(self):
threading.Thread.__init__(self)

def run(self):
print "ChildThread.run(): ", os.getpid()
mymod.blockall()
while (True):
print "ChildThread before sleep(10)"
time.sleep(10)
print "ChildThread after sleep(10)"

class MainThread(object):

def __init__(self):
self.flag = threading.Event()
self.cthrd = ChildThread()
self.cthrd.setDaemon(True)

def run(self):
print "MainThread.run(): ", os.getpid()
self.flag.wait()
self.cthrd.start()
while (self.flag.isSet()):
print "MainThread before sleep(15)"
time.sleep(15)
print "MainThread after sleep(15)"
return # or cleanup routine

if __name__ == "__main__":

# Normally, the process is daemonized first. That's been
# left out for testing purposes.

signal.signal(signal.SIGINT, sigHandle)
signal.signal(signal.SIGQUIT, sigHandle)
signal.signal(signal.SIGTERM, sigHandle)

mthrd = MainThread()
print "Starting up"
mthrd.flag.set()
mthrd.run()
print "Shutting down"

sys.exit(0)

#--- CODE END ---#
Nov 16 '06 #2

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

Similar topics

3
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...
2
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,...
23
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...
1
by: Frank Bossy | last post by:
Dear group :) I don't quite understand the meaning of this paragraph in the qt docu (http://doc.trolltech.com/3.1/threads.html): ***SNIP The Signals and Slots mechanism can be used in...
4
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...
0
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...
0
by: Lloyd Zusman | last post by:
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...
10
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005 and .net 2.0. I started 2 threadpool threads. How do I know when they're done with their tasks? Thanks. ThreadPool.QueueUserWorkItem(new...
6
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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...
0
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,...

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.