Created
May 30, 2011 16:13
-
-
Save splee/999099 to your computer and use it in GitHub Desktop.
An example for the #gevent IRC channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gevent.queue import Queue | |
from gevent.event import Event | |
import gevent | |
import logging | |
# The event which brings everything to a halt | |
stop_event = Event() | |
# set up logging | |
logging.basicConfig(filename='expire.log', level=logging.DEBUG) | |
log_queue = Queue() | |
def log_writer(): | |
while not stop_event.is_set(): | |
if not log_queue.empty(): | |
log = log_queue.get() | |
logging.debug(log) | |
gevent.sleep() | |
def poll(): | |
log_queue.put('starting poll') | |
i = 0 | |
while not stop_event.is_set(): | |
gevent.sleep(0.5) | |
log_queue.put("iteration %d" % i) | |
i += 1 | |
log_worker = gevent.spawn(log_writer) | |
poll_worker = gevent.spawn(poll) | |
# why do I only ever see activity in expire.log when I run either | |
log_worker.join() | |
# or | |
poll_worker.join() | |
# I would expect the greenlets to run in the background like a thread |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment