473,503 Members | 3,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Designing a cancellable function

I have written a function foo() that iterates over and processes
a large number of files. The function should be available to the
user as library function, via a command-line interface, and
through a GUI.

So, I added a 'config' object as a parameter to foo() that can be
used by the caller to explicitly pass in user-defined settings.
Because the processing foo() does can take such a long time, the
next thing I did was add an 'update_function' callback parameter
that foo() will call regularly, so that the GUI can update a
progress bar, and the command-line version can print dots, etc.

I now would also like to add the possibility to allow the user to
*cancel* the execution of foo() during the processing, and I am
wondering what the best / most Pythonic way to design this is.

One obvious approach seems to me to turn the 'config' object into
something more dynamic, and have foo() regularly inspect it to
see if somebody in the main thread has set e.g. config.abort to
True.

Another approach would be to turn foo() into a proper (threaded)
class with distinct run() and abort() methods. But the caller
would still need to register the update callback somehow, and I
am wondering if this way the whole API for foo() won't become to
complex and overdesigned.

I was wondering if anybody has any insights or best practice
recommendations for me here. Do I keep the function interface? Do
I use a class? Any other solution I am overlooking?

Many thanks in advance for your advice.

--
Leo Breebaart <le*@lspace.org>
Dec 16 '06 #1
3 1398
Hi Leo,

what about one (or more) thread(s) which run the eat() method of
FilesEater object with inheriths from a superclass what's needed to
update the progress attribute (for example a _inc_progress() private
method)? (So you can have a UrlEater class and so on, them all knowing
how to update their progress)
Then you give FilesEater (or maybe a better name) a reference to a
config dict at init time.

You can have multiple FilesEater with different configurations and you
can handle the problem of stopping execution with a stopEating()
public method which sets an internal flag (a Lock object) and you
check it inside the object runloop with something like:

def eat(self):
for f in self.files:
do_some_stuff()
self._eat_for_real()
do_some_other_stuff()

def _eat_for_real(): # you should use the with statement inside here =)
self._eatLock.acquire()
do_the_processing
_write_the_result() # !
self._eatLock.release()

with this you can pause and restart the execution by calling
pause/restart methods that acquire and release the eatLock. It writes
the result before releasing it so you know it finishes the last chunk
when you suspend it.
If then you want to abord, just call the pause() on every *Eater
object and exit.
If you want to exit immediately just exit and design the code that
reads the output of the eat() processing in a way that it can
recognize broken chunks, delete them and go on (if they are written
on-disk obviously).

Hope this helps,
Michele
On 16 Dec 2006 01:20:47 GMT, Leo Breebaart <le*@lspace.orgwrote:
I have written a function foo() that iterates over and processes
a large number of files. The function should be available to the
user as library function, via a command-line interface, and
through a GUI.

So, I added a 'config' object as a parameter to foo() that can be
used by the caller to explicitly pass in user-defined settings.
Because the processing foo() does can take such a long time, the
next thing I did was add an 'update_function' callback parameter
that foo() will call regularly, so that the GUI can update a
progress bar, and the command-line version can print dots, etc.

I now would also like to add the possibility to allow the user to
*cancel* the execution of foo() during the processing, and I am
wondering what the best / most Pythonic way to design this is.

One obvious approach seems to me to turn the 'config' object into
something more dynamic, and have foo() regularly inspect it to
see if somebody in the main thread has set e.g. config.abort to
True.

Another approach would be to turn foo() into a proper (threaded)
class with distinct run() and abort() methods. But the caller
would still need to register the update callback somehow, and I
am wondering if this way the whole API for foo() won't become to
complex and overdesigned.

I was wondering if anybody has any insights or best practice
recommendations for me here. Do I keep the function interface? Do
I use a class? Any other solution I am overlooking?

Many thanks in advance for your advice.

--
Leo Breebaart <le*@lspace.org>
--
http://mail.python.org/mailman/listinfo/python-list
Dec 16 '06 #2
At Friday 15/12/2006 22:20, Leo Breebaart wrote:
>I have written a function foo() that iterates over and processes
a large number of files. The function should be available to the
user as library function, via a command-line interface, and
through a GUI.

So, I added a 'config' object as a parameter to foo() that can be
used by the caller to explicitly pass in user-defined settings.
Because the processing foo() does can take such a long time, the
next thing I did was add an 'update_function' callback parameter
that foo() will call regularly, so that the GUI can update a
progress bar, and the command-line version can print dots, etc.

I now would also like to add the possibility to allow the user to
*cancel* the execution of foo() during the processing, and I am
wondering what the best / most Pythonic way to design this is.
I can't say if this is the "best/more Pythonic way", but a simple way
would be to use the return value from your callback. Consider it an
"abort" function: if it returns True, cancel execution; as long as it
returns False, keep going.
(The somewhat "reversed" meaning is useful in case the user doesn't
provide a callback at all, or it's an empty one, or it contains just
a print ".", statement, all of these returning False; so, to actually
abort the process it must have an explicit "return True" statement).
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! - http://correo.yahoo.com.ar
Dec 16 '06 #3
Gabriel Genellina wrote:
>I now would also like to add the possibility to allow the user to
*cancel* the execution of foo() during the processing, and I am
wondering what the best / most Pythonic way to design this is.

I can't say if this is the "best/more Pythonic way", but a simple way
would be to use the return value from your callback. Consider it an
"abort" function: if it returns True, cancel execution; as long as it
returns False, keep going.
It's even easier if the callback function simply raise an exception, which can
be caught from the outside:
class AbortOperationError(RuntimeError):
pass
def callback(N):
updateProgressBar(N)
processGUIEvents()
if exit_button_pressed:
raise AbortOperationError()
try:
longFunction(callback)
except AbortOperationError()
pass
def longFunction(callback):
for i in xrange(1000000000):
# do something
callback(i / 1000000000.0)
--
Giovanni Bajo
Dec 16 '06 #4

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

Similar topics

3
1594
by: eric rudolph | last post by:
1. I'm designing a PHP-based page that has a lot of design stuff in it. It's very tough to mix the HTML and PHP and have it be readable. The site isn't overall so complicated, there's just a lot of...
6
6123
by: shailesh kumar | last post by:
Hi, I need to design data interfaces for accessing files of very large sizes efficiently. The data will be accessed in chunks of fixed size ... My data interface should be able to do a random...
6
2122
by: E G | last post by:
Hi! I am having problems in designing a class. First, I have a base class that allocates a 3D data set and allows some other mathematical operations with it, something like this: template...
13
16700
by: Charulatha Kalluri | last post by:
Hi, I'm implementing a Matrix class, as part of a project. This is the interface I've designed: class Matrix( )
5
2028
by: Verde | last post by:
I'm using a 3rd party component in an ASP.NET 1.1 Web application. The component has a .Click event that can be fired from the client, with an associated event procedure in the code-behind module....
1
1238
by: Ben Kim | last post by:
Hello all, Prior to our days with .NET (yesterday - grin), we could LoadLibrary any application DLL and call a function/procedure. If we stored our Class DLL's in the GAC (since we are...
3
1871
by: mystilleef | last post by:
Hello, I need to design a plug-in system for a project. The goal is to allow third party developers interact with an application via plug-ins in a clean and robust manner. At this point I am...
5
1583
by: The Cool Giraffe | last post by:
I'm designing an ABC and in connection to that i have run into some "huh!" and "oh...". Let me put it as a list. 1. Since the class will only contain bodies of the methods, only the header file...
9
2612
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the...
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7315
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...
1
6974
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...
1
4991
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...
0
3158
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...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
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 ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
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...

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.