[Tutor] how to get the return value?

seedseven at home.nl seedseven at home.nl
Mon Mar 6 13:24:03 CET 2006


---- Danny Yoo <dyoo at hkn.eecs.berkeley.edu> schrijft: 

> But since the scheduler runs in a separate loop than the rest of your
> program, trying to return a value between the two won't work very well.
> 
> 
> However, there are other approaches: we can use some kind of shared
> container or communication channel between the main() and the scheduler,
> so that they can communicate.
> 
> 
> One possible way they can communicate is with a shared Queue:
> 
>     http://www.python.org/doc/lib/module-Queue.html
> 
> Here is an example that may help:
> 
> ######################################################################
> from sched import scheduler
> import time
> from Queue import Queue
> from threading import Thread
> 
> 
> s = scheduler(time.time, time.sleep)
> q = Queue()
> PRIORITY = 1
> 
> def f(n):
>     """Pushes n into the queue, and then reschedules itself with
>     n+1 to run five seconds later"""
>     q.put(n)
>     s.enter(5, PRIORITY, f, (n+1,))
> 
> ## Let's prime things up.  We'll set up the scheduler, and then have
> ## it run on its own daemon thread.
> s.enter(5, PRIORITY, f, (0,))
> t = Thread(target=s.run)
> t.setDaemon(True)
> t.start()
> 
> ## At this point, our main thread of execution is separate from the
> ## scheduler thread t.
> while True:
>     print "Waiting for event on queue."
>     print q.get()
> ######################################################################
> 
> 
> Does this make sense?

Not yet completly Danny, I'll have to study it a bit, but from your use of threads I think you have a better understanding of what I want than I have myself.

Thanks,

Ingo


More information about the Tutor mailing list