Deferring a function call

T

TomF

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
 
S

Steven D'Aprano

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.
 
L

Lawrence D'Oliveiro

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)
 
C

Cameron Simpson

M

Michael Ricordeau

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,
 
P

Peter Otten

TomF said:
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
 
T

TomF

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
 
W

Werner Thie

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top