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 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
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
> 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."
> 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
> 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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);
|
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
|
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...
|
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...
|
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...
| |
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.
|
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.
|
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...
|
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>
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |