Function stopping a function

S

Sorin Schwimmer

Hi All,

We all know that a function can launch the execution
of another function. How can a function stop the
execution of another function?

For instance, lenghty_function() executes, when an
external event triggers cancel(), which is supposed to
abruptly stop lengthy_function(), reset some variables
and exit immediately.

Thanks for your advice
SxN


____________________________________________________________________________________
Be a better sports nut! Let your teams follow you
with Yahoo Mobile. Try it now. http://mobile.yahoo.com/sports;_ylt=At9_qDKvtAbMuh1G1SQtBI7ntAcJ
 
D

Dennis Lee Bieber

Hi All,

We all know that a function can launch the execution
of another function. How can a function stop the
execution of another function?
First: explain what you mean by "launch the execution of"?

Unless you are using multiple threads, or spawning subprocesses,
function CALLS are a sequential operation. The caller "jumps into" the
function, the function runs, the function reaches a "return" point, the
caller code resumes.

So... there is NO WAY another function could affect the running code
EXCEPT by being in another thread, or via some sort of interrupt
mechanism (VMS asynchronous system traps). Even these methods can NOT
directly do anything with the interrupted function -- at best they can
set a flag (a common variable, a signal bit/event flag [Amiga/VMS
respectively]) which the interrupted function, once it resumes,
explicitly checks and exits.

Even threads require something of the same format -- while Windows
(for example) does have a low-level call to force the death of a thread,
even its documentation is that using it can leave all sorts of
corruption in the program.
For instance, lenghty_function() executes, when an
external event triggers cancel(), which is supposed to
abruptly stop lengthy_function(), reset some variables
and exit immediately.
And here is the crux -- your external event is either handled as a
system interrupt, or via a separate thread... the only way to stop
"lengthy_function" is for it to do something like set a flag "Die =
True", and for lengthy_function to be in a loop --

while not Die:
do a short bit of work
go back to test Die and ...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Duncan Booth

Sorin Schwimmer said:
For instance, lenghty_function() executes, when an
external event triggers cancel(), which is supposed to
abruptly stop lengthy_function(), reset some variables
and exit immediately.

def lenghty_function(some, arguments, abort=lambda: False):
while not abort():
do some part of the function


then you just pass in whatever abort function is appropriate: it can check
some flag to see whether the function should continue, or it might just
check the current time and return True if the function has been going too
long.
Just make sure that the badly spelled function calls abort() often.
 
H

hdante

Note, this only works in Unix systems:

import os, signal

def long_process():
while True: print "I'm messing with your terminal ! ",

def short_process(long_process_id):
raw_input('Press [Enter] to kill the bad process')
os.kill(long_process_id, signal.SIGKILL)
print
print 'Hehe !'

def main():
print 'Starting two processes (press [Enter]): '
raw_input()
pid = os.fork()
if (pid != 0):
short_process(pid)
else:
long_process()

main()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top