Skip to content

Instantly share code, notes, and snippets.

@SavinaRoja
Created September 3, 2018 13:58
Show Gist options
  • Save SavinaRoja/140fdee61a98def92adbdf5b504264ec to your computer and use it in GitHub Desktop.
Save SavinaRoja/140fdee61a98def92adbdf5b504264ec to your computer and use it in GitHub Desktop.
A demonstration of how a Queue can be used to sync requests over serial
import asyncio
import functools
class Router(object):
def __init__(self, n):
self.serial_queue = asyncio.Queue()
self.n = n
async def serial_executor(self):
count = 0
while True:
await asyncio.sleep(0)
if not self.serial_queue.empty():
handler_f, return_queue = await self.serial_queue.get()
reply = await handler_f()
await return_queue.put(reply)
count += 1
if count == self.n:
break
async def handler(self, msg, return_queue):
await self.serial_queue.put(
(functools.partial(self.printer, msg), return_queue))
async def printer(self, msg):
await asyncio.sleep(0.1) # A little bit of work
return 'Printed: {}'.format(msg)
THE_BIG_N = 100
r = Router(THE_BIG_N)
zqueue = asyncio.Queue()
print(zqueue)
async def client(n):
count = 0
sent_count = 0
while True:
if sent_count < n:
print('client: {}'.format(sent_count))
await r.handler(str(sent_count), zqueue)
sent_count += 1
await asyncio.sleep(0)
if not zqueue.empty():
reply = await zqueue.get()
print(reply)
count += 1
if count == n:
break
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(r.serial_executor(), client(THE_BIG_N)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment