Managing timing in Python calls

Discussion in 'Python' started by Ross, Dec 15, 2008.

  1. Ross

    Ross Guest

    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.
    Ross, Dec 15, 2008
    #1
    1. Advertising

  2. Ross

    Guest

    On 15 Dic, 16:21, Ross <> 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
    , Dec 15, 2008
    #2
    1. Advertising

  3. Ross

    Ross Guest

    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.
    Ross, Dec 15, 2008
    #3
  4. Ross

    Guest

    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.
    , Dec 15, 2008
    #4
  5. Ross

    Ross Guest

    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.



    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.
    Ross, Dec 17, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honne Gowda A
    Replies:
    2
    Views:
    860
    Karl Heinz Buchegger
    Oct 31, 2003
  2. andy6
    Replies:
    2
    Views:
    752
    andy6 via DotNetMonster.com
    Jun 9, 2006
  3. Richard Tobin
    Replies:
    24
    Views:
    772
  4. Merennulli
    Replies:
    0
    Views:
    81
    Merennulli
    Sep 8, 2006
  5. Bob
    Replies:
    5
    Views:
    253
Loading...

Share This Page