Added eventlet examles: examples/simple_eventlet_receive.py simple_eventlet.send.py

This commit is contained in:
Ask Solem 2010-12-01 12:28:31 +01:00
parent b6d679fe34
commit 97ac8b077c
2 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,47 @@
"""
Example that sends a single message and exits using the simple interface.
You can use `simple_receive.py` (or `complete_receive.py`) to receive the
message sent.
"""
import eventlet
from eventlet import spawn
from Queue import Empty
from kombu import BrokerConnection
eventlet.monkey_patch()
def wait_many(timeout=1):
#: Create connection
#: If hostname, userid, password and virtual_host is not specified
#: the values below are the default, but listed here so it can
#: be easily changed.
connection = BrokerConnection(hostname="localhost",
userid="guest",
password="guest",
virtual_host="/")
#: SimpleQueue mimics the interface of the Python Queue module.
#: First argument can either be a queue name or a kombu.Queue object.
#: If a name, then the queue will be declared with the name as the queue
#: name, exchange name and routing key.
queue = connection.SimpleQueue("kombu_demo")
while True:
try:
message = queue.get(block=False, timeout=timeout)
except Empty:
break
else:
spawn(message.ack)
print(message.payload)
spawn(wait_many).wait()

View File

@ -0,0 +1,42 @@
"""
Example that sends a single message and exits using the simple interface.
You can use `simple_receive.py` (or `complete_receive.py`) to receive the
message sent.
"""
import eventlet
from kombu import BrokerConnection
eventlet.monkey_patch()
def send_many(n):
#: Create connection
#: If hostname, userid, password and virtual_host is not specified
#: the values below are the default, but listed here so it can
#: be easily changed.
connection = BrokerConnection(hostname="localhost",
userid="guest",
password="guest",
virtual_host="/")
#: SimpleQueue mimics the interface of the Python Queue module.
#: First argument can either be a queue name or a kombu.Queue object.
#: If a name, then the queue will be declared with the name as the queue
#: name, exchange name and routing key.
queue = connection.SimpleQueue("kombu_demo")
def send_message(i):
queue.put({"hello": "world%s" % (i, )})
pool = eventlet.GreenPool(10)
for i in xrange(n):
pool.spawn(send_message, i)
pool.waitall()
if __name__ == "__main__":
send_many(10)