Issue
Is it possible to change the control from one async function to another or from one async function to a sync function?
import asyncio
def main():
print("Beginning")
asyncio.run(async_func())
print ("Middle")
print("End")
async def async_func():
print(1)
await asyncio.sleep(1)
# I want to print "Middle" during this
print(2)
await asyncio.sleep(1)
# I want to print "End" during this
print(3)
main()
Solution
You can't. Apart from control structures, statements in a function are still executed top to bottom. Async doesn't change that. You can't "transfer control" to the middle of another function.
Answered By - Thomas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.