Skip to content

Instantly share code, notes, and snippets.

@Integralist
Created July 4, 2016 10:41
Show Gist options
  • Save Integralist/6f34e23f71340a1a23e846cd2f64cf32 to your computer and use it in GitHub Desktop.
Save Integralist/6f34e23f71340a1a23e846cd2f64cf32 to your computer and use it in GitHub Desktop.
Python Asyncio Loop Forever
import asyncio
async def listener():
while True:
message = await sqs.poll()
if message:
asyncio.ensure_future(handle(message))
async def handler(message):
await ...
loop = asyncio.get_event_loop()
asyncio.ensure_future(listener)
loop.run_forever()
@Integralist
Copy link
Author

Web Socket version and Queue

@asyncio.coroutine
def consumer(queue):
    while True:
        message = yield from queue.get()
        yield from handle(message)

@asyncio.coroutine
def listener(queue):
    for i in range(5):
         asyncio.ensure_future(consumer(queue))
    while True:
        message = yield from websocket.recieve_message()
        if message:
            yield from q.put(message)

q = asyncio.Queue()
loop = asyncio.get_event_loop()
loop.run_until_complete(listener(q))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment