@async注解使用代码
时间: 2023-08-15 09:13:26 浏览: 169
Spring中@Async注解执行异步任务的方法
5星 · 资源好评率100%
在Python中,@asyncio.coroutine和@asyncio.coroutine装饰器已经被弃用,使用@async和async def代替。下面是一个使用@async装饰器的示例代码:
```
import asyncio
@asyncio.coroutine
def coroutine_func():
yield from asyncio.sleep(1)
print("Coroutine function completed")
@asyncio.coroutine
def main():
print("Starting coroutine function")
yield from coroutine_func()
print("Coroutine function finished")
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```
使用@async装饰器的示例代码:
```
import asyncio
async def coroutine_func():
await asyncio.sleep(1)
print("Coroutine function completed")
async def main():
print("Starting coroutine function")
await coroutine_func()
print("Coroutine function finished")
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```
阅读全文