4 ms·
I'm not sure why the example starts with all those verbose calls to run_until_complete(). Why not do this? async def main(): db = DB()
by BiteCode_dev 5y ago
I'm not sure why the example starts with all those verbose calls to run_until_complete(). Why not do this?
async def main():
db = DB()
await db.connect('db_name', {
'host': 'localhost',
'port': '5432',
'database': 'db_name',
'username': 'db_user_name',
'password': 'db_user_password'
}, default=True)
await db.connect('other_db_name', {
'host': 'localhost',
'port': '5432',
'database': 'other_db_name',
'username': 'db_user_name',
'password': 'db_user_password'
}, default=False)
db.connection('other_db_name')
db.default = 'other_db_name'
await db.stop()
asyncio.run(main())
Am I missing something ? It's using asyncio.run() in other examples, so it assumes 3.7+ anyway.
- uranusjr 5y agoThey differ slightly in semantics; asyncio.run() always creates a new event loop to run the task, while run_until_complete() don’t (unless you call asyncio.create_event_loop() of course). I don’t know if this is the reason behind the decision, but the two are not equivalent (and there are cases asyncio.run() wouldn’t work).
- BiteCode_dev 5y agoYes but await doesn't, which is what I replaced run_until_complete() with here. The asyncio.run() is just to pretend I run the lib snippet in a code that already started the loop, which the doc seems to do.