import itertools
from twisted.internet import defer
from twisted.internet import reactor
from twisted.internet import task
FREQUENCY = 10
def startProducing(q):
"""Put an incrementing number in the queue every 1/FREQUENCY
seconds.
"""
counter = itertools.count()
def produce():
q.put(counter.next())
task.LoopingCall(produce).start(1.0/FREQUENCY)
def startConsuming(q):
"""Consume an object from the queue and print it.
"""
def consumer(o):
print o
q.get().addCallback(consumer)
q.get().addCallback(consumer)
q = defer.DeferredQueue()
startProducing(q)
startConsuming(q)
reactor.run()
Monday, February 22, 2010
Twisted's DeferredQueue
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment