Issue
I was wondering is any reason to declare a piece of code which does not await
anything as async def
(coroutine) rather than def
(function)? Python explicitly allows this.
def i_am_sync():
return 42
async def i_am_async():
return 42
I know that the coroutine version could be scheduled on the event loop, but I can not imagine any practical application of this, as you could just invoke the synchronous code directly?
Solution
If function doesn't contain async stuff and you think it would stay this way, there's no need to declare it async
. One can argue that it's even better not to make function async without some reason since to separate sync world from async better (if you really interested why, you may want to read this article and especially conclusion "How To Live With Coloured Functions").
Note however that "async stuff" may be not only some network I/O related, but other things like CPU-heavy operations or Disk-I/O which you may want to run in thread or process and await for the result.
Answered By - Mikhail Gerasimov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.