Deferring a function call

Steven D'Aprano steve-REMOVE-THIS at cybersource.com.au
Tue Oct 19 00:59:59 EDT 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



More information about the Python-list mailing list