Deferring a function call

Discussion in 'Python' started by TomF, Oct 19, 2010.

  1. TomF

    TomF Guest

    I'm writing a simple simulator, and I want to schedule an action to
    occur at a later time. Basically, at some later point I want to call a
    function f(a, b, c). But the values of a, b and c are determined at
    the current time.

    One way way to do this is to keep a list of entries of the form [[TIME,
    FN, ARGS]...] and at simulated time TIME do: apply(FN, ARGS)
    Aside from the fact that apply is deprecated, it seems like there
    should be a cleaner (possibly more Pythonic) way to do this. Ideas?

    -Tom
     
    TomF, Oct 19, 2010
    #1
    1. Advertising

  2. TomF

    Chris Rebert Guest

    On Mon, Oct 18, 2010 at 9:21 PM, TomF <> wrote:
    > I'm writing a simple simulator, and I want to schedule an action to occur at
    > a later time.  Basically, at some later point I want to call a function f(a,
    > b, c).  But the values of a, b and c are determined at the current time.


    See the `sched` std lib module:
    http://docs.python.org/library/sched.html

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Oct 19, 2010
    #2
    1. Advertising

  3. TomF

    Chris Rebert Guest

    On Mon, Oct 18, 2010 at 9:21 PM, TomF <> wrote:
    > I'm writing a simple simulator, and I want to schedule an action to occur at
    > a later time.  Basically, at some later point I want to call a function f(a,
    > b, c).  But the values of a, b and c are determined at the current time.


    See the `sched` std lib module:
    http://docs.python.org/library/sched.html

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Oct 19, 2010
    #3
  4. On Mon, 18 Oct 2010 21:21:41 -0700, TomF wrote:

    > I'm writing a simple simulator, and I want to schedule an action to
    > occur at a later time. Basically, at some later point I want to call a
    > function f(a, b, c). But the values of a, b and c are determined at the
    > current time.
    >
    > One way way to do this is to keep a list of entries of the form [[TIME,
    > FN, ARGS]...] and at simulated time TIME do: apply(FN, ARGS) Aside from
    > the fact that apply is deprecated,



    Instead of apply(f, args) you should write:

    f(*args)

    If you have keyword arguments as well (or instead):

    f(*args, **kwargs)


    > it seems like there should be a
    > cleaner (possibly more Pythonic) way to do this. Ideas?


    Chris Rebert has already mentioned the sched module. Otherwise, put the
    function call in a thread, and have the thread use time.sleep to wait
    until the right time to execute.



    --
    Steven
     
    Steven D'Aprano, Oct 19, 2010
    #4
  5. In message <2010101821214168010-tomfsessile@gmailcom>, TomF wrote:

    > One way way to do this is to keep a list of entries of the form [[TIME,
    > FN, ARGS]...] and at simulated time TIME do:


    fn(*args) or fn(**kwargs)
     
    Lawrence D'Oliveiro, Oct 19, 2010
    #5
  6. On 19Oct2010 04:59, Steven D'Aprano <> wrote:
    | Chris Rebert has already mentioned the sched module. Otherwise, put the
    | function call in a thread, and have the thread use time.sleep to wait
    | until the right time to execute.

    There is also the Timer class from the threading module.

    Cheers,
    --
    Cameron Simpson <> DoD#743
    http://www.cskk.ezoshosting.com/cs/

    A crash reduces
    your expensive computer
    to a simple stone.
    - Haiku Error Messages http://www.salonmagazine.com/21st/chal/1998/02/10chal2.html
     
    Cameron Simpson, Oct 19, 2010
    #6
  7. For scheduling, I use eventlet package and spawn_after_local .
    http://eventlet.net

    But you must be aware of the constraints like using "monkey patched" modules .




    Le Mon, 18 Oct 2010 21:21:41 -0700,
    TomF <> a écrit :

    > I'm writing a simple simulator, and I want to schedule an action to
    > occur at a later time. Basically, at some later point I want to call a
    > function f(a, b, c). But the values of a, b and c are determined at
    > the current time.
    >
    > One way way to do this is to keep a list of entries of the form [[TIME,
    > FN, ARGS]...] and at simulated time TIME do: apply(FN, ARGS)
    > Aside from the fact that apply is deprecated, it seems like there
    > should be a cleaner (possibly more Pythonic) way to do this. Ideas?
    >
    > -Tom
    >
     
    Michael Ricordeau, Oct 19, 2010
    #7
  8. TomF

    Peter Otten Guest

    TomF wrote:

    > I'm writing a simple simulator, and I want to schedule an action to
    > occur at a later time. Basically, at some later point I want to call a
    > function f(a, b, c). But the values of a, b and c are determined at
    > the current time.
    >
    > One way way to do this is to keep a list of entries of the form [[TIME,
    > FN, ARGS]...] and at simulated time TIME do: apply(FN, ARGS)
    > Aside from the fact that apply is deprecated, it seems like there
    > should be a cleaner (possibly more Pythonic) way to do this. Ideas?


    You can prepare the function

    fstar = functools.partial(f, a, b, c)

    and later invoke it as

    fstar()

    Peter
     
    Peter Otten, Oct 19, 2010
    #8
  9. TomF

    TomF Guest

    Thanks for the ideas, everyone.

    functools.partial and lambda expressions seem like a more pythonic way
    of doing what I want. I don't know whether they're actually more
    efficient or better, but at least they eliminate the need to carry args
    around separately.

    I'd forgotten Python has a sched module in its standard library. It
    may be overkill for what I want to do but I'll take a look.

    -Tom
     
    TomF, Oct 19, 2010
    #9
  10. TomF

    Werner Thie Guest

    For me nothing beats using twisted ( http://www.twistedmatrix.com ) with
    its clean implementation of deferreds and a host of other useful things
    for simulations besides being the 'Swiss Army Knife' of networking in
    Python. If you throw in stackless ( http://www.stackless.com )
    simulations with extremely high counts of participants and/or fine
    granularity distributed across multiple machines are fun to write,
    operate and maintain.

    Werner


    On 10/18/10 6:21 PM, TomF wrote:
    > I'm writing a simple simulator, and I want to schedule an action to
    > occur at a later time. Basically, at some later point I want to call a
    > function f(a, b, c). But the values of a, b and c are determined at the
    > current time.
    >
    > One way way to do this is to keep a list of entries of the form [[TIME,
    > FN, ARGS]...] and at simulated time TIME do: apply(FN, ARGS)
    > Aside from the fact that apply is deprecated, it seems like there should
    > be a cleaner (possibly more Pythonic) way to do this. Ideas?
    >
    > -Tom
    >
     
    Werner Thie, Oct 19, 2010
    #10
    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. v4vijayakumar
    Replies:
    8
    Views:
    399
    mlimber
    May 30, 2006
  2. grbgooglefan
    Replies:
    2
    Views:
    439
    Pascal Bourguignon
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    4
    Views:
    458
    Kenny McCormack
    Jan 30, 2008
  4. grbgooglefan
    Replies:
    0
    Views:
    410
    grbgooglefan
    Jan 30, 2008
  5. Imobach González Sosa

    Deferring proc bindings

    Imobach González Sosa, Jan 12, 2011, in forum: Ruby
    Replies:
    2
    Views:
    89
    Imobach González Sosa
    Jan 13, 2011
Loading...

Share This Page