30 lines
598 B
Python
30 lines
598 B
Python
# -*- encoding:utf-8 -*-
|
||
|
||
'''
|
||
@Author : dingjiawen
|
||
@Date : 2023/12/6 16:20
|
||
@Usage : asyncio库 可以使用async和await关键字
|
||
@Desc :异步爬虫测试 定义协程
|
||
@参考:https://github.dev/Python3WebSpider/AsyncTest
|
||
'''
|
||
import asyncio
|
||
|
||
|
||
async def execute(x):
|
||
print('Number:', x)
|
||
return x
|
||
|
||
# 创建一个协程对象 coroutine
|
||
coroutine = execute(1)
|
||
|
||
print('Coroutine:', coroutine)
|
||
print('After calling execute')
|
||
|
||
loop = asyncio.get_event_loop()
|
||
|
||
task = loop.create_task(coroutine)
|
||
print('Task:', task)
|
||
loop.run_until_complete(task)
|
||
print('Task:', task)
|
||
print('After calling loop')
|