Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import asyncio
  2. import time
  3. from motor.motor_asyncio import AsyncIOMotorClient
  4.  
  5.  
  6. async def get_all_from_coll(col):
  7. client = AsyncIOMotorClient("localhost", 27017)
  8. db = client.project_matrix
  9. cursor = db[col].find()
  10. time.sleep(5)
  11. for document in await cursor.to_list(length=100):
  12. print(document)
  13.  
  14.  
  15. loop = asyncio.get_event_loop()
  16. print('001')
  17. loop.run_until_complete(get_all_from_coll('users'))
  18. print('002')
  19.  
  20. >>>001
  21. >>>{'_id': ObjectId('58d9b178d011b53743d44413'), 'username': 'test1', 'password': 'test', '__v': 0}
  22. >>>{'_id': ObjectId('58d9b229d011b53743d44414'), 'username': 'test2', 'password': 'test', '__v': 0}
  23. >>>002
  24.  
  25. for document in await cursor.to_list(length=100):
  26. # (wrong)
  27.  
  28. async for document in cursor.limit(100):
  29. # (ok)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement