代码:
import asyncio
import functools
async def func1(x,y):
print("compute:{0}+{1}".format(x,y))
await asyncio.sleep(1)
return x+y
async def func2(x,y):
print("compute:{0}*{1}".format(x,y))
await asyncio.sleep(1)
return x*y
async def func3(x,y):
task1=asyncio.create_task(func1(x,y))
task2=asyncio.create_task(func2(x,y))
task1.add_done_callback(functools.partial(end1,x,y))
task2.add_done_callback(functools.partial(end2,x,y))
await asyncio.sleep(6)
def end1(x,y,t1):
print("{0}+{1}={2}".format(x,y,t1.result()))
def end2(x,y,t2):
print("{0}*{1}={2}".format(x, y, t2.result()))
if __name__=="__main__":
loop=asyncio.get_event_loop()
loop.run_until_complete(func3(2,3))
loop.close()
问题:
老师请问一下,当代码执行到func3中的task1=asyncio.create_task(func1(x,y))语句的时候,此时代码会切换到func1方法中,当运行到func1方法中的await.asyncio.sleep(1)的时候会睡眠1秒,那么在这1秒的时间里,程序是一直停在func1,当睡眠结束以后执行return x+y? 还是去执行func3中的task2=asyncio.create_task(func2(x,y))?