Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Managing timing in Python calls

Reply
Thread Tools

Managing timing in Python calls

 
 
Ross
Guest
Posts: n/a
 
      12-15-2008

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.
 
Reply With Quote
 
 
 
 
bieffe62@gmail.com
Guest
Posts: n/a
 
      12-15-2008
On 15 Dic, 16:21, Ross <(E-Mail Removed)> 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




 
Reply With Quote
 
 
 
 
Ross
Guest
Posts: n/a
 
      12-15-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> 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
>


The wxTimerEvent does sound attractive - I'll look into that.

Thanks too for the opinion on loading images - gives me some guts to
just give it a try without threading it and see how it goes.

I appreciate the quick input

Ross.
 
Reply With Quote
 
cmdrrickhunter@yaho.com
Guest
Posts: n/a
 
      12-15-2008
I believe WxTimerEvent is handled using the event queue, which isn't
going to do what you want. An event which goes through the queue does
not get processed until you return to the queue.

What you want to do is actually a rather difficult task to do
generically. Should the task be interrupted immediately? Or is a
tiny latency acceptable? Should the function being terminated get to
handle its own termination? Or should the termination be forced on
it. What sort of overhead is acceptable for this "set_timeout"
behavior?

I would not be surprised if there isn't a built in solution, because
its so hard, but rather built in tools which can be used to do it.

If your timeouts are on the order of seconds, you might be able to
just check time.time() at the begining, and compare it to the current
time later in the function. This could be on the main thread or on a
worker thread.

If you need better handling, you may want to look at how condition
variables and such work.

Finally, thread has a function to send a Keyboard Interrupt to the
main thread. I believe you could do your work on the main thread, and
catch the interrupt.

"Background" tasks are not easy to implement in any language (other
than perhaps AJAX ).

Remember, Python does not support truly simultaneous threads. It
actually does timeslices of about 100 operations. Any solution you
choose should work given this information.

And as for a "nicer" construct, I personally just learned of how to
handle the "with" command. I could see something like

class Timeout:
def __init__(self, t):
self.t = t
def __enter__(self):
self.start = time.time()
def __exit__(self, x, y, z):
return None
def __nonzero__(self):
return time.time() - self.start <= self.t


def doSomethingLong(timeout = True): # true guarentees bailout never
occurs
while timeout:
doAnIteration()

with Timeout(3) as t:
doSomethingLong(t)



and have your Timeout class have a flag which it sets when
doSomethingLong needs to bail out, using whatever method is "best" for
your particular application. This is, of course pseudocode - I've not
run it through python msyself. Hopefully any errors are obvious
enough that you can work around them.
 
Reply With Quote
 
Ross
Guest
Posts: n/a
 
      12-17-2008
Interesting stuff - I hadn't come across the 'with' syntax before, so
I've learned something already.

I was briefly excited to learn about the callLater command which is just
a convenience class for the wxTimer class. It seems to be pretty much
a parallel of the
var t = window.setTimeout( function () { do_when_timed_out}

sort of thing in AJAX.

However, as is well grumbled on the 'net, you can't use wxTimer from a
non-main thread. So that dropped off my plate.

But getting my head around my AJAX problem versus my python
implementation, I realized my use of those javascript structures were
really just used because javascript doesn't allow any threading at all.

With Python, just having my other processing path in a thread is enough,
and I can use the brutish time.sleep() function, without worrying about
blocking the processing of my mainline (UI) thread. So I'm able to proceed.

I do want to know more about the 'with' command tho' so I'll look into that.

Thx again.

Ross.



(E-Mail Removed) wrote:
> I believe WxTimerEvent is handled using the event queue, which isn't
> going to do what you want. An event which goes through the queue does
> not get processed until you return to the queue.
>
> What you want to do is actually a rather difficult task to do
> generically. Should the task be interrupted immediately? Or is a
> tiny latency acceptable? Should the function being terminated get to
> handle its own termination? Or should the termination be forced on
> it. What sort of overhead is acceptable for this "set_timeout"
> behavior?
>
> I would not be surprised if there isn't a built in solution, because
> its so hard, but rather built in tools which can be used to do it.
>
> If your timeouts are on the order of seconds, you might be able to
> just check time.time() at the begining, and compare it to the current
> time later in the function. This could be on the main thread or on a
> worker thread.
>
> If you need better handling, you may want to look at how condition
> variables and such work.
>
> Finally, thread has a function to send a Keyboard Interrupt to the
> main thread. I believe you could do your work on the main thread, and
> catch the interrupt.
>
> "Background" tasks are not easy to implement in any language (other
> than perhaps AJAX ).
>
> Remember, Python does not support truly simultaneous threads. It
> actually does timeslices of about 100 operations. Any solution you
> choose should work given this information.
>
> And as for a "nicer" construct, I personally just learned of how to
> handle the "with" command. I could see something like
>
> class Timeout:
> def __init__(self, t):
> self.t = t
> def __enter__(self):
> self.start = time.time()
> def __exit__(self, x, y, z):
> return None
> def __nonzero__(self):
> return time.time() - self.start <= self.t
>
>
> def doSomethingLong(timeout = True): # true guarentees bailout never
> occurs
> while timeout:
> doAnIteration()
>
> with Timeout(3) as t:
> doSomethingLong(t)
>
>
>
> and have your Timeout class have a flag which it sets when
> doSomethingLong needs to bail out, using whatever method is "best" for
> your particular application. This is, of course pseudocode - I've not
> run it through python msyself. Hopefully any errors are obvious
> enough that you can work around them.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Voipdiscount Outgoing Calls with Sipgate Incoming Calls on a Linksys PAP2 Giganews UK VOIP 27 10-09-2006 11:28 PM
Redirecting javascript calls or managing them in an iframe. Merennulli Javascript 0 09-08-2006 05:51 PM
ods calls business object then method calls ta with output params andy6 ASP .Net 2 06-09-2006 01:54 AM
MoVoIP - FREE MOBILE Inetrnet Phone Calls - FREE Internet Phone Calls ubifone VOIP 0 07-29-2005 04:31 PM
Sequence of constructor calls and destructor calls. Honne Gowda A C++ 2 10-31-2003 09:31 AM



Advertisments