python协程获取返回值

python协程获取返回值,第1张

一、通过ensure_future获取,本质是future对象中的result方法
import asyncio
from functools import partial

async def talk(name):
	print("talk running!!!")
	await asyncio.sleep(1)
	return f"{name} talk"

async def sleep(work):
	print("sleep running!!!")
	await asyncio.sleep(0.1)
	return f"{work} sleep"

if __name__ == "__main__":
	# 通过ensure_future获取,本质是future对象中的result方法
	loop = asyncio.get_event_loop()
	get_future1 = asyncio.ensure_future(talk("Dog"))
	get_future2 = asyncio.ensure_future(sleep("Cat"))
	loop.run_until_complete(get_future1)
	loop.run_until_complete(get_future2)
	print(get_future1.result())
	print(get_future2.result())

打印结果:

talk running!!!
sleep running!!!
Dog talk
Cat sleep
二、使用loop自带的create_task,获取返回值
import asyncio
from functools import partial

async def talk(name):
	print("talk running!!!")
	await asyncio.sleep(1)
	return f"{name} talk"

async def sleep(work):
	print("sleep running!!!")
	await asyncio.sleep(0.1)
	return f"{work} sleep"

if __name__ == "__main__":
	# 使用loop自带的create_task,获取返回值
	loop = asyncio.get_event_loop()
	task1 = loop.create_task(talk("Dog"))
	task2 = loop.create_task(sleep("Cat"))
	loop.run_until_complete(task1)
	loop.run_until_complete(task2)
	print(task1.result())
	print(task2.result())

打印结果:

talk running!!!
sleep running!!!
Dog talk
Cat sleep
三、使用callback,一旦await地方的内容运行完,就会运行callback
import asyncio
from functools import partial

async def tack(name):
	print("talk running!!!")
	await asyncio.sleep(1)
	return f"{name} talk"

async def sleep(work):
	print("sleep running!!!")
	await asyncio.sleep(0.1)
	return f"{work} sleep"

def callback(value):
	print(f"主动抛出接收返回值:{value}")

if __name__ == "__main__":
	# 使用callback,一旦await地方的内容运行完,就会运行callback
	loop = asyncio.get_event_loop()
	task1 = loop.create_task(tack("Dog"))
	task2 = loop.create_task(sleep("Cat"))
	task1.add_done_callback(callback)
	task2.add_done_callback(callback)
	loop.run_until_complete(task1)
	loop.run_until_complete(task2)
	print(task1.result())
	print(task2.result())
	

打印结果:

talk running!!!
sleep running!!!
主动抛出接收返回值:<Task finished name='Task-2' coro=<sleep() done, defined at D:/python_files/py2022_02/Case01/ceshi.py:24> result='Cat sleep'>
主动抛出接收返回值:<Task finished name='Task-1' coro=<tack() done, defined at D:/python_files/py2022_02/Case01/ceshi.py:19> result='Dog talk'>
Dog talk
Cat sleep
四、使用partial这个模块向callback函数中传入值
import asyncio
from functools import partial

async def talk(name):
	print("talk running !!!")
	await asyncio.sleep(1)
	return f"{name} talk"

async def sleep(work):
	print("sleep running!!!")
	await asyncio.sleep(0.1)
	return f"{work} sleep"
	
def callback(value):
    print(f"主动抛出接收返回值:{value}")

if __name__ == "__main__":
	# 使用partial这个模块向callback函数中传入值
	loop = asyncio.get_event_loop()
	task1 = loop.create_task(talk("Dog"))
	task2 = loop.create_task(sleep("Cat"))
	task1.add_done_callback(partial(callback))
	task2.add_done_callback(partial(callback))
	loop.run_until_complete(task1)
	loop.run_until_complete(task2)
	print(task1.result())
	print(task2.result())

打印结果:

talk running !!!
sleep running!!!
主动抛出接收返回值:<Task finished name='Task-2' coro=<sleep() done, defined at D:/python_files/py2022_02/Case01/ceshi.py:25> result='Cat sleep'>
主动抛出接收返回值:<Task finished name='Task-1' coro=<talk() done, defined at D:/python_files/py2022_02/Case01/ceshi.py:19> result='Dog talk'>
Dog talk
Cat sleep

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/873482.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存