473,782 Members | 2,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a signal to terminate a programm running with an asyncore loop

I'm developing a program that runs using an asyncore loop. Right now
I can adequately terminate it using Control-C, but as things get
refined I need a better way to stop it. I've developed another
program that executes it as a child process using popen2.Popen4() . I
was attempting to use signals to stop it (using os.kill()) but I keep
running into a problem where sending the signal causes an infinite
loop of printing the message "warning: unhandled exception". This
message appears to be coming from asyncore.py.

Searching online I've found this old post that describes setting up a
signal handler using signal.signal() :

http://mail.python.org/pipermail/med...00/000571.html

I've tried it but I don't see any indication that my handler method is
being called. My best guess is that something somewhere else is
overriding my signal handling callbacks and sending the signals
elsewhere until they make their way into asyncore where they cause the
error message.

Any other ideas on how to get this to work? If there's a different
signal I can use that other code won't override, that's fine (I don't
care what the signal is, as long as I can catch it). Or perhaps there
is something different I can do? The program, at present, doesn't
have much in the way of an internal shutdown mechanism.

I'm using Python 2.5.1 on Fedora 8; the program does not need to be
portable to Windows.

- David

(p.s. please post replies on the list; this email address doesn't work
so I won't see any replies sent directly to me)
Feb 7 '08 #1
5 6649
da***@quanteriu m.zzn.com wrote:
I'm developing a program that runs using an asyncore loop. Right now
I can adequately terminate it using Control-C, but as things get
refined I need a better way to stop it. I've developed another
program that executes it as a child process using popen2.Popen4() . I
was attempting to use signals to stop it (using os.kill()) but I keep
running into a problem where sending the signal causes an infinite
loop of printing the message "warning: unhandled exception". This
message appears to be coming from asyncore.py.

Searching online I've found this old post that describes setting up a
signal handler using signal.signal() :

http://mail.python.org/pipermail/med...00/000571.html

I've tried it but I don't see any indication that my handler method is
being called. My best guess is that something somewhere else is
overriding my signal handling callbacks and sending the signals
elsewhere until they make their way into asyncore where they cause the
error message.

Any other ideas on how to get this to work? If there's a different
signal I can use that other code won't override, that's fine (I don't
care what the signal is, as long as I can catch it). Or perhaps there
is something different I can do? The program, at present, doesn't
have much in the way of an internal shutdown mechanism.

I'm using Python 2.5.1 on Fedora 8; the program does not need to be
portable to Windows.

- David

(p.s. please post replies on the list; this email address doesn't work
so I won't see any replies sent directly to me)
Please share a little of the code you used to "catch" the signal. What
signal(s) are you catching? What kill level are you sending (these have to
match up). I've used this quite successfully in some of my programs.

-Larry

Feb 8 '08 #2
On Feb 8, 7:04 am, Larry Bates <larry.ba...@we bsafe.comwrote:
da...@quanteriu m.zzn.com wrote:
I'm developing a program that runs using an asyncore loop. Right now
I can adequately terminate it using Control-C, but as things get
refined I need a better way to stop it. I've developed another
program that executes it as a child process using popen2.Popen4() . I
was attempting to use signals to stop it (using os.kill()) but I keep
running into a problem where sending the signal causes an infinite
loop of printing the message "warning: unhandled exception". This
message appears to be coming from asyncore.py.
Searching online I've found this old post that describes setting up a
signal handler using signal.signal() :
http://mail.python.org/pipermail/med...00/000571.html
I've tried it but I don't see any indication that my handler method is
being called. My best guess is that something somewhere else is
overriding my signal handling callbacks and sending the signals
elsewhere until they make their way into asyncore where they cause the
error message.
Any other ideas on how to get this to work? If there's a different
signal I can use that other code won't override, that's fine (I don't
care what the signal is, as long as I can catch it). Or perhaps there
is something different I can do? The program, at present, doesn't
have much in the way of an internal shutdown mechanism.
I'm using Python 2.5.1 on Fedora 8; the program does not need to be
portable to Windows.
- David
(p.s. please post replies on the list; this email address doesn't work
so I won't see any replies sent directly to me)

Please share a little of the code you used to "catch" the signal. What
signal(s) are you catching? What kill level are you sending (these have to
match up). I've used this quite successfully in some of my programs.

-Larry
Sure. First, here is the signal I'm sending, from the parent process:

os.kill(self.ch ild.pid, signal.SIGTERM)

self.child is the popen2.Popen4 object returned from creating the
child process.

In the child process, one of the first things in the "main" section
(if __name__ == '__main__':) is to set up the signal handler:

signal.signal(s ignal.SIGTERM, handle_shutdown _signal)

handle_shutdown _signal looks a lot like Sam Rushing's example code
from his 2000 post, though I didn't see much of a reason to have the
callback function turn around and call another function, so I combined
them:

SHUTDOWN_PERFOR MED = False

def handle_shutdown _signal(signum, frame):
global SHUTDOWN_PERFOR MED
print 'in shutdown handler, caught signal', signum #diagnostic msg
if not SHUTDOWN_PERFOR MED:
#do some stuff
asyncore.socket _map.clear()
SHUTDOWN_PERFOR MED = True
raise asyncore.ExitNo w

I also tried replacing the raise asyncore.ExitNo w command with a
simple exit() call but that didn't help.

I've also tried using SIGHUP and SIGKILL and they didn't seem to make
a difference.

- David
Feb 8 '08 #3
da***@quanteriu m.zzn.com wrote:
On Feb 8, 7:04 am, Larry Bates <larry.ba...@we bsafe.comwrote:
>da...@quanteri um.zzn.com wrote:
>>I'm developing a program that runs using an asyncore loop. Right now
I can adequately terminate it using Control-C, but as things get
refined I need a better way to stop it. I've developed another
program that executes it as a child process using popen2.Popen4() . I
was attempting to use signals to stop it (using os.kill()) but I keep
running into a problem where sending the signal causes an infinite
loop of printing the message "warning: unhandled exception". This
message appears to be coming from asyncore.py.
Searching online I've found this old post that describes setting up a
signal handler using signal.signal() :
http://mail.python.org/pipermail/med...00/000571.html
I've tried it but I don't see any indication that my handler method is
being called. My best guess is that something somewhere else is
overriding my signal handling callbacks and sending the signals
elsewhere until they make their way into asyncore where they cause the
error message.
Any other ideas on how to get this to work? If there's a different
signal I can use that other code won't override, that's fine (I don't
care what the signal is, as long as I can catch it). Or perhaps there
is something different I can do? The program, at present, doesn't
have much in the way of an internal shutdown mechanism.
I'm using Python 2.5.1 on Fedora 8; the program does not need to be
portable to Windows.
- David
(p.s. please post replies on the list; this email address doesn't work
so I won't see any replies sent directly to me)
Please share a little of the code you used to "catch" the signal. What
signal(s) are you catching? What kill level are you sending (these have to
match up). I've used this quite successfully in some of my programs.

-Larry

Sure. First, here is the signal I'm sending, from the parent process:

os.kill(self.ch ild.pid, signal.SIGTERM)

self.child is the popen2.Popen4 object returned from creating the
child process.

In the child process, one of the first things in the "main" section
(if __name__ == '__main__':) is to set up the signal handler:

signal.signal(s ignal.SIGTERM, handle_shutdown _signal)

handle_shutdown _signal looks a lot like Sam Rushing's example code
from his 2000 post, though I didn't see much of a reason to have the
callback function turn around and call another function, so I combined
them:

SHUTDOWN_PERFOR MED = False

def handle_shutdown _signal(signum, frame):
global SHUTDOWN_PERFOR MED
print 'in shutdown handler, caught signal', signum #diagnostic msg
if not SHUTDOWN_PERFOR MED:
#do some stuff
asyncore.socket _map.clear()
SHUTDOWN_PERFOR MED = True
raise asyncore.ExitNo w

I also tried replacing the raise asyncore.ExitNo w command with a
simple exit() call but that didn't help.

I've also tried using SIGHUP and SIGKILL and they didn't seem to make
a difference.

- David

When signal is caught handle_shutdown _signal is called. At that point
SHUTDOWN_PERFOR MED will ALWAYS be False. Normally all you do in this function
is to set SHUTDOWN_PERFOR MED to True and have a test somewhere in your main
program loop that tests if SHUTDOWN_PERFOR MED:... I know that when you reach

if not SHUTDOWN_PERFOR MED:

SHUTDOWN_PERFOR MED will always be False so #do some stuff will get executed.

Setting SHUTDOWN_PERFOR MED = True doesn't do any good unless you catch this back
in the main program.

If I were you I would try to issue kill from console first to see if it works.
Then try os.kill().

Hope this helps some.

-Larry
Feb 9 '08 #4
On Feb 8, 4:03*pm, Larry Bates <larry.ba...@we bsafe.comwrote:
When signal is caught handle_shutdown _signal is called. *At that point
SHUTDOWN_PERFOR MED will ALWAYS be False. *Normally all you do in this function
is to set SHUTDOWN_PERFOR MED to True and have a test somewhere in your main
program loop that tests if SHUTDOWN_PERFOR MED:... *I know that when you reach

if not SHUTDOWN_PERFOR MED:

SHUTDOWN_PERFOR MED will always be False so #do some stuff will get executed.

Setting SHUTDOWN_PERFOR MED = True doesn't do any good unless you catch this back
in the main program.

If I were you I would try to issue kill from console first to see if it works.
Then try os.kill().

Hope this helps some.

-Larry
I won't have a chance to try your suggestions until next week. Though
I'm not convinced that handle_shutdown _signal is even getting called,
since I'm not seeing the diagnostic print statement, neither on the
console nor where stdout/stderr is being redirected. I did try using
the kill command from the console to issue SIGTERM to the process
(e.g. kill -s SIGTERM <pid>) and I still got the infinite series of
"warning: unhandled exception" messages.

- David
Feb 9 '08 #5
On Feb 8, 4:03 pm, Larry Bates <larry.ba...@we bsafe.comwrote:
When signal is caught handle_shutdown _signal is called. At that point
SHUTDOWN_PERFOR MED will ALWAYS be False. Normally all you do in this function
is to set SHUTDOWN_PERFOR MED to True and have a test somewhere in your main
program loop that tests if SHUTDOWN_PERFOR MED:... I know that when you reach

if not SHUTDOWN_PERFOR MED:

SHUTDOWN_PERFOR MED will always be False so #do some stuff will get executed.

Setting SHUTDOWN_PERFOR MED = True doesn't do any good unless you catch this back
in the main program.

If I were you I would try to issue kill from console first to see if it works.
Then try os.kill().

Hope this helps some.

-Larry
Sorry for the slow response. I had other tasks come up last week that
took priority. Got back to this today and tried it. I simplified my
code so right now the signal handler looks like this:

def handle_shutdown _signal(signum, frame):
print 'in shutdown handler, caught signal', signum
asyncore.socket _map.clear()
exit()

I'm still registering the handler the same way:

signal.signal(s ignal.SIGTERM, handle_shutdown _signal)

I'm still not convinced the handler function is getting called. I
tried killing the process from the command line (# kill -s SIGTERM
<pid>) and my print message doesn't get shown; and running ps ax shows
the program is still running.

I'm wondering if there is something else going on. I've checked my
code and nothing else registers any signal handlers, but there are
places that use asyncore.dispat cher and asynchat to handle some
network communication; perhaps something in there re-registers the
signal handlers?

I tried writing a simple test program; running it from one shell and
killing it from another works fine:

#!/usr/bin/env python
import signal
def handler(signum, frame):
print 'caught signal', signum
exit()
signal.signal(s ignal.SIGTERM, handler)
while True:
a = 1
print 'out'

After I issue the fill command, I get the following output:

caught signal 15

Which is what I expected; since the handler calls exit() I didn't
expect to see the 'out' message ever get printed.

Any thoughts?

- David
Feb 19 '08 #6

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

Similar topics

2
510
by: duane voth | last post by:
> #to control asyncore > while 1: > asyncore.loop(.1) > if not fromgui.empty(): > #handle messages from GUI The above doesn't work for me - asyncore.loop(<anything>) always blocks. The asyncore.poll(timeout=0.01) functions however works fine. Thus I have: while 1:
0
1868
by: fishboy | last post by:
Howdy, Sorry if this is a double post. First try seemed to go into hyperspace. I'm working on a personal project. It's going to be a multipart binary attachment downloader that will search alternate servers for missing pieces. This is the working code so far. It will walk a newsgroup and download and decode all the single part attachments. Just change server,user,password,group at the bottom to something less
3
5264
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not have a programmable exit condition. It keeps looping till the channels in its socket map (a dictionary) are closed and don't have any pending reads/writes. If you are using Python threads in your application, by using either the threading or...
0
1757
by: Tony Meyer | last post by:
Changes in asyncore from 2.3 to 2.4 mean that asyncore.poll() now passes all the sockets in the map to select.select() to be checked for errors, which is probably a good thing. If an error occurs, then handle_expt() is called, which by default logs the error. asyncore.dispatcher creates nonblocking sockets. When connect_ex() is called on a nonblocking socket, it will probably return EWOULDBLOCK (connecting takes time), which may mean...
11
2974
by: Jackie | last post by:
Hi everyone, I'd like to know when and how signals are used (e.g. SIGFPE, SIGABRT, SIGTERM, SIGSEGV, SIGINT)? Thank you so much.
3
1490
by: Edward Elliott | last post by:
I'm having trouble using the smptd module. The docs are woefully inadequate and inspecting the source didn't help either. So far I've figured out how to subclass smtpd.SMTPServer and override the process_message method to handle smtp messages. I create an instance of my server and it listens on the given interface. I can connect to the port it's on and send SMTP commands but it doesn't respond. I've verified that it's actually bound...
7
11082
by: Adrian Casey | last post by:
I have a multi-threaded python application which uses pexpect to connect to multiple systems concurrently. Each thread within my application is a connection to a remote system. The problem is when one of the child threads runs a command which generates an unlimited amount of output. The classic example of this is the "yes" command. If you execute "pexpect.run('yes')", your cpu will sit at 100% forever. Here is a simple multi-threaded...
5
7107
by: JamesHoward | last post by:
I have a problem with python's asyncore module throwing a bad file descriptor error. The code might be difficult to copy here, but the problem is essentially: The server wants to sever the connection of an open Asyncore socket. Calling the socket.close() nor the socket.shutdown(2) calls seem to work. The only way I can close the connection without creating the error below is to have the client close the connection. I have the...
0
1140
by: codewarrior1241 | last post by:
Hi all, I will try to be as clear as possible with my application, maybe ppl familiar with asyncore can help me out :-) Basically, I have python 2.5 in SuSe running with a server written using asynchat. I use it to connect to several clients inside the asyncore.loop construct. This part of the application works fine. However, along with talking to clients through sockets, I need to talk to some Spread groups, obviously using spread. My...
0
9641
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...
1
10080
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
9944
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
8968
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...
1
7494
isladogs
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
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
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
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.