473,548 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

module signal

Hello,

consider the following two programs:

# (1)

import sys, signal

def alarm_handler(s ignum, frame):
raise

try:
signal.signal(s ignal.SIGALRM, alarm_handler)
signal.alarm(3)
n = 0
while 1:
print n
n = n+1
except:
print "Time over."

############### ############### ############### ############### #

# (2)

import sys, signal

def alarm_handler(s ignum, frame):
raise

try:
signal.signal(s ignal.SIGALRM, alarm_handler)
signal.alarm(3)
re.search("a((( .)*c)*d)*e", "abcdf"*20)
except:
print "Time over."

############### ############### ############### ############### #

(1) behaves the way one would expect it to behave: It stops counting
after about 3 seconds. (2) will stop trying to match the regexp at
once. And this behaviour will not change if you give any other
argument to signal.alarm().

Why?

Another program of mine contains code that is fairly analogous to (2)
(or at least I thought so). Here it is:
# (3)

def main():
# read in a corpus file
# compile some regexp patterns
# and then, for each pattern do:

for line in corpus:
try:
signal.signal(s ignal.SIGALRM, alarm_handler)
signal.alarm(10 )
test = pattern.search( line) #!
if not test:
out.append(line )
else:
out.append(some _function(line, pattern))
except:
out.append(line )
############### ############### ############### ############### #

The regexps pattern in code (3) use to be rather complicated. When
code (3) gets really long lines as input, Python will be executing
line #! for days. Why?

What I am looking for is code with the following meaning:

for n seconds try to do:
# any Python code may be inserted here
print "bla"

When the n seconds are over, Python should print "bla". It should not
exit the program. How can this be done?
Klaus
Jul 18 '05 #1
5 2290
Klaus Neuner wrote:
# (2)

import sys, signal

def alarm_handler(s ignum, frame):
raise

try:
signal.signal(s ignal.SIGALRM, alarm_handler)
signal.alarm(3)
re.search("a((( .)*c)*d)*e", "abcdf"*20)
except:
print "Time over."

############### ############### ############### ############### #

(1) behaves the way one would expect it to behave: It stops counting
after about 3 seconds. (2) will stop trying to match the regexp at
once. And this behaviour will not change if you give any other
argument to signal.alarm().

Why?


To remind you that a bare except is always a bad idea. Modify your program
along these lines

class Timeout(Excepti on): pass

def alarm_handler(s ignum, frame):
raise Timeout

try
...
except Timeout:
print "Time over."

to see that you didn't import the re module which raises a NameError.
Unfortunately the resulting script does not terminate anymore and I cannot
help you with that.

Peter

Jul 18 '05 #2
Hi Klaus,
What I am looking for is code with the following meaning:

for n seconds try to do:
# any Python code may be inserted here
print "bla"

When the n seconds are over, Python should print "bla". It should not
exit the program. How can this be done?

It should work with a signal alarm; I don't know why it
doesn't. You could also try a Timer object from the
threading class; it's even easier than a signal alarm.

Regards,
Josef
Jul 18 '05 #3
> to see that you didn't import the re module which raises a NameError.

O.k. Sorry for that. Sometimes one doesn't see the most obvious things.

So I should reformulate my problem as follows:

Why does (2) not work (though (1) does)?

# (1)

import sys, signal
class Timeout(Excepti on):
pass

def alarm_handler(s ignum, frame):
raise Timeout

try:
signal.signal(s ignal.SIGALRM, alarm_handler)
signal.alarm(3)
n = 0
while 1:
print n
n = n+1
except Timeout:
print "Time over."

############### ############### ############### ############### #

# (2)

import sys, signal, re

class Timeout(Excepti on):
pass

def alarm_handler(s ignum, frame):
raise Timeout

try:
signal.signal(s ignal.SIGALRM, alarm_handler)
signal.alarm(3)
re.search("a((( .)*c)*d)*e", "abcdf"*20)
except Timeout:
print "Time over."
Jul 18 '05 #4
> doesn't. You could also try a Timer object from the
threading class; it's even easier than a signal alarm.


I don't see how to do it with a Timer object only. Could you rewrite
the regexp example with a Timer object such that it works?

Klaus
Jul 18 '05 #5
> I don't see how to do it with a Timer object only. Could you rewrite
the regexp example with a Timer object such that it works?

Oh yes, I'm wrong. I confused this module with something else, sorry.
Jul 18 '05 #6

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

Similar topics

0
1551
by: Berteun Damman | last post by:
Hello, First I was trying to get PyOSD, but as soon as I did `import pyosd' Python received a SIGABRT. Then I wrote my own module, which looks like: #include <Python.h> static PyMethodDef testmod_methods = { {NULL, NULL} }; void initmymod(void) { Py_InitModule("mymod", testmod_methods);
1
2586
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
8
1980
by: Steve Erickson | last post by:
I have a logger class that uses the Python logging module. When I call it within a program using the unittest module, I get one line in the log file for the first test, two identical ones for the second, etc. I'm using local variables, which should go out of context with each test. Setting the 'propagate' property to False doesn't have any...
5
2160
by: Mothra | last post by:
Hi All, I am the current author of the Astro-Sunrise perl module http://search.cpan.org/~rkhill/Astro-Sunrise-0.91/Sunrise.pm and was wondering if it would be worth while to convert it to python. First off, I have never programmed in python. I would like to use this project to learn python. I was wondering if there was a "How to program...
3
2666
by: LeTubs | last post by:
Hi I'm not sure if this is correct ...place to post but here goes This is what i'm trying to do, I want to write a signal / alarm handler ( I don't know which hence the posting here, as once I know the proper name /useage then I'll use google to try and find out more information ).... Thus any tips or pointers would be helpful (and I'm...
11
2937
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.
5
2044
by: Bo Peng | last post by:
Dear list, I have not done a thorough test, but it occurs to me that 1. python code can be interrupted by Ctrl-C. 2. A C module, if I add a main() function and run independently, can be interrupted by Ctrl-C. 3. If I load the C module in python and run, the program will not respond to Ctrl-C interruption. I have to kill python explicitly.
23
2036
by: Harishankar | last post by:
Hi, Sorry to start off on a negative note in the list, but I feel that the Python subprocess module is sorely deficient because it lacks a mechanism to: 1. Create non-blocking pipes which can be read in a separate thread (I am currently writing a mencoder GUI in Tkinter and need a full fledged process handler to control the command line...
0
1804
by: John [H2O] | last post by:
There's a lot of greek for me here ... should I post to numpy-discussions as well??? The backtrace is at the bottom.... Thanks! GNU gdb Fedora (6.8-21.fc9) Copyright (C) 2008 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
0
7512
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...
0
7438
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...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7803
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...
0
6036
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...
0
3495
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...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
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...

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.