Managing timing in Python calls

bieffe62 at gmail.com bieffe62 at gmail.com
Mon Dec 15 17:19:31 CET 2008


On 15 Dic, 16:21, Ross <nos... at forMe.thks> wrote:
> I'm porting some ugly javascript managed stuff to have an equivalent
> behaviour in a standalone app. It uses events that arrive from a server,
> and various small images.  In this standalone version, the data is local
> in a file and the images in a local directory.
>
> My AJAX code managed a timely presentation of the info, and in the
> Javascript that relied on the ugly:
>
>         myImage.onload = function(){dosomething_when_it's_finished}
>
> structure. Also, I used the similarly unpretty:
>
>         var t = window.setTimeout( function () { do_when_timed_out}
>
> structures which allows stuff to happen after a perscribed period.
>
> In my python implementation my first guess is to use a thread to load my
> image into a variable
>
>       myImage = wx.Image("aPic.gif",
>                 wx.BITMAP_TYPE_GIF ).ConvertToBitmap()
>
> so that it won't block processing. (Though perhaps it'll just happen so
> fast without a server involved that I won't care.)
>
> Is there a nice equivalent of a 'setTimeout' function in python? ie to
> call a function after some time elapses without blocking my other
> processing?  I suppose just a thread with a time.sleep(x_mS) in it would
> be my first guess?
>
> Can anyone give me some feedback on whether that's a logical path
> forward, or if there are some nicer constructs into which I might look?
>
> Thanks for any suggests... Ross.

Python has in its standard library a timer class which actually is
implemented as a thread (I think) ...
however, when using a GUI package, I think it is better to use gui-
specific functions for event-driven programming,
to make sure that your code do not mess with GUI event loop and to
work around the lack  of thread-safety in some GUI libraries.
This applies to timer/timeouts but also to execute code when specific
I/O events occur ( e.g. the receiving of data from a socket ).

Although I'm not an expert of  pywx, a quick search pointed me to this
page:

http://wxpython.org/onlinedocs.php

from which it seams that WxTimerEvent couldbe what you need.

I agree with you that for loading images from local files a thread
should not be needed.

P.S : notice that the documentation refers to the C++ library on which
the python wrapper is built. This is often the case for
python wrapper of GUI libraries. However, the most important ones come
with a rich set of demo programs (and pywx demo suite is quite
complete) from which one can lear what he needs.

Ciao
-----
FB







More information about the Python-list mailing list