Skip to content

Instantly share code, notes, and snippets.

@hasenj
Created April 10, 2013 07:42
Show Gist options
  • Select an option

  • Save hasenj/5352608 to your computer and use it in GitHub Desktop.

Select an option

Save hasenj/5352608 to your computer and use it in GitHub Desktop.

Revisions

  1. hasenj created this gist Apr 10, 2013.
    36 changes: 36 additions & 0 deletions timeout_demo.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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)