Last active
September 19, 2024 12:35
-
-
Save rudyryk/974c0e1498743a0b7ae6 to your computer and use it in GitHub Desktop.
Simple message queue demo on Redis LPUSH / BRPOP for Python 3.3+ and asyncio.
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
# No Rights Reserved | |
# http://creativecommons.org/publicdomain/zero/1.0/ | |
"""Simple message queue demo on Redis LPUSH / BRPOP for Python 3.3+ and asyncio. | |
See also "Pattern: Reliable queue" at http://redis.io/commands/rpoplpush to get | |
an idea for improvement. | |
""" | |
import asyncio | |
import aioredis | |
import datetime | |
import random | |
loop = asyncio.get_event_loop() | |
redis_send = None | |
redis_recv = None | |
now = lambda: datetime.datetime.now().strftime("[%H:%M:%S.%f]") # %Y-%m-%d %H:%M:%S | |
@asyncio.coroutine | |
def connect(): | |
global redis_send, redis_recv | |
redis_send = yield from aioredis.create_redis( | |
('localhost', 6379), loop=loop, db=10) | |
redis_recv = yield from aioredis.create_redis( | |
('localhost', 6379), loop=loop, db=10) | |
@asyncio.coroutine | |
def sender(): | |
counter = 1 | |
while True: | |
print(now(), 'send %s' % counter) | |
redis_send.lpush('mylist', '%s' % counter) | |
yield from asyncio.sleep(random.random() * 2.5) | |
counter += 1 | |
@asyncio.coroutine | |
def receiver(): | |
while True: | |
val = yield from redis_recv.brpop('mylist', timeout=0) | |
print(now(), 'recv %s' % val) | |
if __name__ == '__main__': | |
loop.run_until_complete(connect()) | |
asyncio.async(receiver()) | |
loop.run_until_complete(sender()) | |
redis_send.close() | |
redis_recv.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment