Skip to content

Instantly share code, notes, and snippets.

@m-x-k
Created January 3, 2018 09:57
Show Gist options
  • Save m-x-k/dbef2b518ae7788751f14a609a0681b6 to your computer and use it in GitHub Desktop.
Save m-x-k/dbef2b518ae7788751f14a609a0681b6 to your computer and use it in GitHub Desktop.
python 3 asyncio example: periodic check mongodb collection names
import asyncio
from pymongo import MongoClient
@asyncio.coroutine
def periodic(wait_seconds=2):
while True:
names = client['local'].collection_names()
for name in names:
print(name)
yield from asyncio.sleep(wait_seconds)
def stop():
task.cancel()
task = asyncio.Task(periodic(wait_seconds=2))
if __name__ == '__main__':
client = MongoClient('localhost', 27017)
loop = asyncio.get_event_loop()
loop.call_later(60, stop) # Breaks out after 1 minute
try:
loop.run_until_complete(task)
except asyncio.CancelledError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment