Created
January 3, 2018 09:57
-
-
Save m-x-k/dbef2b518ae7788751f14a609a0681b6 to your computer and use it in GitHub Desktop.
python 3 asyncio example: periodic check mongodb collection names
This file contains 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
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