473,583 Members | 3,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

another try: "timout actions"

Hello,

as I am desperately searching for a (straightforwar d) solution of the
following problem, I'll give it another try:

How can I tell Python to do things of the following kind:

for n seconds try to do:
somefunc()
other_func()

somefunc() should be allowed to imply ANY activity Python is
capable to do. When the n seconds are over, Python should not exit the
program but execute other_func().

It turned out that the module signal is a solution for some but not
for all instances of somefunc(): If somefunc() contains a regexp match
that does not terminate (in a reasonable time), then the program will
not go on to other_func() (after n seconds). You can verify this by
running the programs (1) and (2) given below.

So, what other ways are there to code actions of the type described
above? Which way would be the cleanest and most straightforward ? (I
don't want to use a program call, because this would decrease
readability, portability and other good properties a lot.)

Thanks in advance and sorry for asking the same question twice.

Klaus
############### ############### ############### ############### #

# (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 #1
1 1278
So, what other ways are there to code actions of the type described
above? Which way would be the cleanest and most straightforward ? (I
don't want to use a program call, because this would decrease
readability, portability and other good properties a lot.)


The only thing I can think of is to delegate the work to a therad, and wait
the specified period of time. If by then the result is computed, use it.
Otherwise continue - and ignore the thread. I admit that that is a ugly
solution - especially when the computation is time intensive.

Maybe some gurus can elaborate on why the regexp-search isn't interrupted by
the signal - the only thing I can think of is that the signal is deferred
until the interpreter re-enters its bytecode-loop - but why the search is
so atomic I don't know.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

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

Similar topics

4
1953
by: Laura | last post by:
Hello, I'm pretty sure that this can be done, but I am unable to find documentation on it. Presently I have a plain HTML survey form where users check multiple checkboxes, then submit. The results are sent via email to the administrator. What I also want to happen is a new confirmation page to open showing
8
3448
by: gregory_may | last post by:
Is there a way to grab a "Screen Shot" that includes "Tool Tips"? I saw this code someplace, cant remember where. But it doesnt grab "Tool Tips". Is there a better way to do this in .net? Thanks! Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput...
28
3393
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
30
2890
by: bblais | last post by:
Hello, Let me start by saying that I am coming from a background using Matlab (or Octave), and C++. I am going to outline the basic nuts-and-bolts of how I work in these languages, and ask for some help to find out how the same thing is done in Python. I am not sure what the standard is. In C++, I open up an editor in one window, a Unix...
3
2667
by: eholz1 | last post by:
Hello PHP programmers. I had a brilliant idea on one of my pages that selects some data from my mysql database. I first set the page up to display some info and an image, just one item, with a row of data, etc. then I thought it would be nice to do a select, and perhaps an update (the title of the image) on the same page.
18
3625
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an abstract class that has the virtual function getId(). I then have a function that prints a special string when a bob meets another bob (all...
3
1752
by: Mark Shroyer | last post by:
I guess this sort of falls under the "shameless plug" category, but here it is: Recently I used a custom metaclass in a Python program I've been working on, and I ended up doing a sort of write-up on it, as an example of what a "real life" __metaclass__ might do for those who may never have seen such a thing themselves. ...
3
3025
by: frosted74 | last post by:
Hi, I have 2 javascript functions, one that sorts a table and one that shows/hides a modal dialog that displays something like "Sorting" with a little icon indicating that the browser is "busy" and you need to wait. This is done because on larger tables the sort routine can take upwards of 10 seconds. I try the following code: <th...
1
2010
by: =?ISO-8859-1?Q?Andr=E9?= | last post by:
Hi everyone, I'd be interested in hearing suggestions as to the "best" way to drive a Python program step by step from another application. Details: --------- I have implemented a "Robot" that can be programmed by a user to perform certain actions. (see Reeborg below for a simple javascript-
0
7895
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
8327
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
8193
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
6579
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
5374
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3818
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...
1
2333
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
1
1433
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1157
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.