I've run into an "opportunit y" 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.isSe t()
mthrd.flag.clea r()
print "flag: ", mthrd.flag.isSe t()
class ChildThread(thr eading.Thread):
def __init__(self):
threading.Threa d.__init__(self )
def run(self):
print "ChildThread.ru n(): ", os.getpid()
while (True):
print "ChildThrea d before sleep(10)"
time.sleep(10)
print "ChildThrea d after sleep(10)"
class MainThread(obje ct):
def __init__(self):
self.flag = threading.Event ()
self.cthrd = ChildThread()
self.cthrd.setD aemon(True)
def run(self):
print "MainThread.run (): ", os.getpid()
self.flag.wait( )
self.cthrd.star t()
while (self.flag.isSe t()):
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(s ignal.SIGINT, sigHandle)
signal.signal(s ignal.SIGQUIT, sigHandle)
signal.signal(s ignal.SIGTERM, sigHandle)
mthrd = MainThread()
print "Starting up"
mthrd.flag.set( )
mthrd.run()
print "Shutting down"
sys.exit(0)
#--- CODE END ---# 1 11337
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(&nma sk);
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 "opportunit y" 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.isSe t()
mthrd.flag.clea r()
print "flag: ", mthrd.flag.isSe t()
class ChildThread(thr eading.Thread):
def __init__(self):
threading.Threa d.__init__(self )
def run(self):
print "ChildThread.ru n(): ", os.getpid()
mymod.blockall( )
while (True):
print "ChildThrea d before sleep(10)"
time.sleep(10)
print "ChildThrea d after sleep(10)"
class MainThread(obje ct):
def __init__(self):
self.flag = threading.Event ()
self.cthrd = ChildThread()
self.cthrd.setD aemon(True)
def run(self):
print "MainThread.run (): ", os.getpid()
self.flag.wait( )
self.cthrd.star t()
while (self.flag.isSe t()):
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(s ignal.SIGINT, sigHandle)
signal.signal(s ignal.SIGQUIT, sigHandle)
signal.signal(s ignal.SIGTERM, sigHandle)
mthrd = MainThread()
print "Starting up"
mthrd.flag.set( )
mthrd.run()
print "Shutting down"
sys.exit(0)
#--- CODE END ---#
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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
|
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 separate threads, as
long as the rules for QObject based classes are followed. The Signals
and Slots mechanism is synchronous: when a signal is emitted, all
slots are called immediately. The slots are executed in the thread
|
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.
| |
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
|
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 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
|
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 WaitCallback(PopulateContextTable));
ThreadPool.QueueUserWorkItem(new WaitCallback(PopulatAdTable));
--
Thanks.
|
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...
|
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...
|
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...
| |
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |