#`async def` vs `def`
1 messages · Page 1 of 1 (latest)
Depends on execution type
async doesn't block main thread
You use async def if you need to await something inside
sync blocks
Just because a function is defined as async it doesn't mean it cannot block
That is, if there is a return in the function, then you need to use async def? Isn't it the other way around?
Return has nothing to do with it
Hm
I said await
Can I have an example?
An example of what?
I just can't figure out what to use
If your db query code is blocking, then it doesn't matter. It's blocking regardless.
And you should switch to an async version of the db driver you're using.
@client.command()
async def command(ctx):
await my_func(id)
async def my_func(id):
name = ... #call database with id
return name
OR
@client.command()
async def command(ctx):
my_func(id)
def my_func(id):
name = ... #call database with id
return name
Quite literally doesn't matter if #call database is a synchronous function.
async def get_member(id: int):
return sync_db.get_member(id)
Still blocking whlie sync_db.get_member() is being executed.
async def get_member(id: int):
return await async_db.get_member(id)
Now it's actually async since the code within the function is also async.
Hm. I use sqlite3. If you about this
Hm, thx