Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This answer has actually two suggestions - my first one and another I have discovered after the first one.</p> <h1>sched</h1> <p>I suspect you are looking for the <a href="http://docs.python.org/library/sched.html" rel="noreferrer"><code>sched</code> module</a>.</p> <p><strong>EDIT</strong>: my bare suggestion seemed little helpful after I have read it. So I decided to test the <code>sched</code> module to see if it can work as I suggested. Here comes my test: I would use it with a sole thread, more or less this way:</p> <pre><code>class SchedulingThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.scheduler = sched.scheduler(time.time, time.sleep) self.queue = [] self.queue_lock = threading.Lock() self.scheduler.enter(1, 1, self._schedule_in_scheduler, ()) def run(self): self.scheduler.run() def schedule(self, function, delay): with self.queue_lock: self.queue.append((delay, 1, function, ())) def _schedule_in_scheduler(self): with self.queue_lock: for event in self.queue: self.scheduler.enter(*event) print "Registerd event", event self.queue = [] self.scheduler.enter(1, 1, self._schedule_in_scheduler, ()) </code></pre> <p>First, I'd create a thread class which would have its own scheduler and a queue. At least one event would be registered in the scheduler: one for invoking a method for scheduling events from the queue.</p> <pre><code>class SchedulingThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.scheduler = sched.scheduler(time.time, time.sleep) self.queue = [] self.queue_lock = threading.Lock() self.scheduler.enter(1, 1, self._schedule_in_scheduler, ()) </code></pre> <p>The method for scheduling events from the queue would lock the queue, schedule each event, empty the queue and schedule itself again, for looking for new events some time in the future. Note that the period for looking for new events is short (one second), you may change it:</p> <pre><code> def _schedule_in_scheduler(self): with self.queue_lock: for event in self.queue: self.scheduler.enter(*event) print "Registerd event", event self.queue = [] self.scheduler.enter(1, 1, self._schedule_in_scheduler, ()) </code></pre> <p>The class should also have a method for scheduling user events. Naturally, this method should lock the queue while updating it:</p> <pre><code> def schedule(self, function, delay): with self.queue_lock: self.queue.append((delay, 1, function, ())) </code></pre> <p>Finally, the class should invoke the scheduler main method:</p> <pre><code> def run(self): self.scheduler.run() </code></pre> <p>Here comes an example of using:</p> <pre><code>def print_time(): print "scheduled:", time.time() if __name__ == "__main__": st = SchedulingThread() st.start() st.schedule(print_time, 10) while True: print "main thread:", time.time() time.sleep(5) st.join() </code></pre> <p>Its output in my machine is:</p> <pre><code>$ python schedthread.py main thread: 1311089765.77 Registerd event (10, 1, &lt;function print_time at 0x2f4bb0&gt;, ()) main thread: 1311089770.77 main thread: 1311089775.77 scheduled: 1311089776.77 main thread: 1311089780.77 main thread: 1311089785.77 </code></pre> <p>This code is just a quick'n'dirty example, it may need some work. However, I have to confess that I am a bit fascinated by the <code>sched</code> module, so did I suggest it. You may want to look for other suggestions as well :)</p> <h1>APScheduler</h1> <p>Looking in Google for solutions like the one I've post, I found this <a href="http://packages.python.org/APScheduler/" rel="noreferrer">amazing APScheduler module</a>. It is so practical and useful that I bet it <em>is</em> your solution. My previous example would be way simpler with this module:</p> <pre><code>from apscheduler.scheduler import Scheduler import time sch = Scheduler() sch.start() @sch.interval_schedule(seconds=10) def print_time(): print "scheduled:", time.time() sch.unschedule_func(print_time) while True: print "main thread:", time.time() time.sleep(5) </code></pre> <p>(Unfortunately I did not find how to schedule an event to execute only once, so the function event should unschedule itself. I bet it can be solved with some decorator.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload