关键就是调用asyncio.run()这个方法
import asyncio
import time
import aiohttp
from celery import Celery
from celery.result import AsyncResult
from lxml import etree
app = Celery('mytasks', backend='redis://localhost', broker='redis://localhost:6379/0')
#async语法的异步爬虫耗时任务
async def async_func():
# 也可以用asyncio.sleep模拟耗时任务
# await asyncio.sleep(3)
# return 'finish'
session = aiohttp.ClientSession()
url = 'https://movie.douban.com/top250'
async with session.get(url) as response:
page_source = await response.text()
html = etree.HTML(page_source)
title_list = html.xpath('//ol[@class="grid_view"]//div[@class="hd"]/a/span[position()=1]/text()')
print(title_list)
return title_list
@app.task(name='mytasks')
def io_func():
#核心代码asyncio.run
return asyncio.run(async_func())
def main():
result = io_func.delay()
return result.id
#循环访问获取异步任务结果
def get_result(task_id):
res = AsyncResult(task_id)
while True:
print(res.status)
if res.ready():
break
time.sleep(1)
return res.result
if __name__ == '__main__':
task_id = main()
print(task_id)
res = get_result(task_id)
print(res)
输出:
c5eec89e-1eff-4a69-8de5-5ad3af934802
PENDING
SUCCESS
['肖申克的救赎', '霸王别姬', '阿甘正传', '这个杀手不太冷', '泰坦尼克号', '美丽人生', '千与千寻', '辛德勒的名单', '盗梦空间', '忠犬八公的故事', '海上钢琴师', '楚门的世界', '三傻大闹宝莱坞', '星际穿越', '机器人总动员', '放牛班的春天', '大话西游之大圣娶亲', '熔炉', '疯狂动物城', '无间道', '龙猫', '教父', '当幸福来敲门', '怦然心动', '触不可及']