Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Deferring a function call

Reply
Thread Tools

Deferring a function call

 
 
TomF
Guest
Posts: n/a
 
      10-19-2010
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

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      10-19-2010
On Mon, Oct 18, 2010 at 9:21 PM, TomF <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      10-19-2010
On Mon, Oct 18, 2010 at 9:21 PM, TomF <(E-Mail Removed)> 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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-19-2010
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
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      10-19-2010
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)
 
Reply With Quote
 
Cameron Simpson
Guest
Posts: n/a
 
      10-19-2010
On 19Oct2010 04:59, Steven D'Aprano <(E-Mail Removed)> 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 <(E-Mail Removed)> 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/ch...2/10chal2.html
 
Reply With Quote
 
Michael Ricordeau
Guest
Posts: n/a
 
      10-19-2010
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 <(E-Mail Removed)> 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
>

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      10-19-2010
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

 
Reply With Quote
 
TomF
Guest
Posts: n/a
 
      10-19-2010
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

 
Reply With Quote
 
Werner Thie
Guest
Posts: n/a
 
      10-19-2010
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
>


 
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
Deferring proc bindings Imobach GonzŠlez Sosa Ruby 2 01-13-2011 05:13 AM
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 4 01-30-2008 05:12 PM
How to call function whose function call with arguments is in astring grbgooglefan C++ 2 01-30-2008 07:18 AM
deferring member object instantiation to constructor v4vijayakumar C++ 8 05-30-2006 01:25 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments