Skip to content

Instantly share code, notes, and snippets.

@hasenj
Created April 10, 2013 07:42
Show Gist options
  • Save hasenj/5352608 to your computer and use it in GitHub Desktop.
Save hasenj/5352608 to your computer and use it in GitHub Desktop.
setTimeout for python. Requires the package `apscheduler` (install via pip).
from apscheduler.scheduler import Scheduler
import datetime as dt
sched = Scheduler()
sched.start()
def timeout(job_fn, *fn_args, **delta_args):
"""Like setTimeout in javascript; returns a job object
First argument is the function to be called.
Positional arguments will be passed to the function when it's called.
Keyword arguemnts will be passed to datetime.timedelta
Usage:
# calls `fn()` after 3 seconds
timeout(fn, seconds=3)
# calls `fn(foo, bar)` after 10 seconds
timeout(fn, foor, bar, seconds=10)
"""
time = dt.datetime.now() + dt.timedelta(**delta_args)
return sched.add_date_job(job_fn, time, fn_args)
# Example usage:
def hello_spam(name):
print "Hello {0}".format(name)
timeout(hello_spam, name, seconds=1)
hello_spam("Dude")
import time
time.sleep(15)
@AndreaFerlini
Copy link

Hey man,
I've tried to run your timeout function on my macbook.
I doesn't work, I have an error like: ImportError: No module named scheduler.

The problem is that I've installed apscheduler via pip install, but it seems like it doesn't recognise it

Thanks for your help.

-A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment