self_example/Spider/Chapter06_异步爬虫/asyncio/asyncTest1.py

30 lines
598 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- 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')