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_functio n' 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 > 3 1409
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_r eal()
do_some_other_s tuff()
def _eat_for_real() : # you should use the with statement inside here =)
self._eatLock.a cquire()
do_the_processi ng
_write_the_resu lt() # !
self._eatLock.r elease()
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.org 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_functio n' 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
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_functio n' 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
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 AbortOperationE rror(RuntimeErr or):
pass
def callback(N):
updateProgressB ar(N)
processGUIEvent s()
if exit_button_pre ssed:
raise AbortOperationE rror()
try:
longFunction(ca llback)
except AbortOperationE rror()
pass
def longFunction(ca llback):
for i in xrange(10000000 00):
# do something
callback(i / 1000000000.0)
--
Giovanni Bajo This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 graphics. Is there some
commercial way to design the page in a design program, then to use the
designed page as a skeleton, or "template" or "fill in" page as a pre-cursor
for a real HTML page? For instance, I could replace the designed graphic...
|
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 seek in the file, as well as sequential access block by
block....
One aspect of the usage of this interface is that there is quite good
chance of accessing same blocks again and again by the application..
|
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 <typename T> class BasicArray
{
|
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( )
|
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.
The logic in the click event procedure may, under some conditions, need to
abort further processing. AFAIK the control's event is not cancellable. At
least its EventArgs does not have any apparent support for .Cancel.
So, what can I do to...
| |
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 designing multiple application executables
that could call common DLL's), is there a similar function in VB.NET that we
can dynamically call / load a DLL and call a method from the resource?
For example (pseudo code):
|
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 overwhelmed by my inexperience with designing plug-in
systems.
Are there any good tutorials on how to design good plug-in
systems with Python, or any language? What are the best
|
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 is needed. There will be no definitions
provided until i derive the ABC. True or false?
2. Since i'll have two different classes (both derived from
the original ABC) i'll use the following syntax in my main
class using the derivation.
|
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 object.The object is triangulated. The
rays can undergo multiple reflections, diffractions etc of the same
object i.e. a ray hits a surface of the object, undergoes reflection
resulting in a reflected ray which can again hit a surface, corner or
edge...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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.
| |