mirror of
https://github.com/shmilylty/OneForAll.git
synced 2026-08-26 12:57:50 +08:00
不使用回调
This commit is contained in:
+13
-12
@@ -1,7 +1,6 @@
|
|||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import functools
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from aiohttp import ClientSession
|
from aiohttp import ClientSession
|
||||||
from aiohttp.resolver import AsyncResolver
|
from aiohttp.resolver import AsyncResolver
|
||||||
@@ -65,14 +64,15 @@ async def fetch(session, url, semaphore):
|
|||||||
return resp, text
|
return resp, text
|
||||||
|
|
||||||
|
|
||||||
def request_callback(future, index, datas):
|
def deal_results(datas, results):
|
||||||
try:
|
for index, result in enumerate(results):
|
||||||
resp, text = future.result()
|
if isinstance(result, Exception):
|
||||||
except BaseException as e:
|
logger.log('DEBUG', result.args)
|
||||||
logger.log('DEBUG', e.args)
|
datas[index]['reason'] = str(result.args)
|
||||||
datas[index]['reason'] = str(e.args)
|
|
||||||
datas[index]['valid'] = 0
|
datas[index]['valid'] = 0
|
||||||
else:
|
continue
|
||||||
|
if isinstance(result, tuple):
|
||||||
|
resp, text = result
|
||||||
datas[index]['reason'] = resp.reason
|
datas[index]['reason'] = resp.reason
|
||||||
datas[index]['status'] = resp.status
|
datas[index]['status'] = resp.status
|
||||||
if resp.status == 400 or resp.status >= 500:
|
if resp.status == 400 or resp.status >= 500:
|
||||||
@@ -92,6 +92,7 @@ def request_callback(future, index, datas):
|
|||||||
datas[index]['title'] = head.text
|
datas[index]['title'] = head.text
|
||||||
else:
|
else:
|
||||||
datas[index]['title'] = text
|
datas[index]['title'] = text
|
||||||
|
return datas
|
||||||
|
|
||||||
|
|
||||||
async def bulk_get_request(datas, port):
|
async def bulk_get_request(datas, port):
|
||||||
@@ -113,11 +114,11 @@ async def bulk_get_request(datas, port):
|
|||||||
for i, data in enumerate(new_datas):
|
for i, data in enumerate(new_datas):
|
||||||
url = data.get('url')
|
url = data.get('url')
|
||||||
task = asyncio.ensure_future(fetch(session, url, semaphore))
|
task = asyncio.ensure_future(fetch(session, url, semaphore))
|
||||||
task.add_done_callback(functools.partial(request_callback,
|
|
||||||
index=i,
|
|
||||||
datas=new_datas))
|
|
||||||
tasks.append(task)
|
tasks.append(task)
|
||||||
if tasks: # 任务列表里有任务不空时才进行解析
|
if tasks: # 任务列表里有任务不空时才进行解析
|
||||||
await asyncio.wait(tasks) # 等待所有task完成
|
# 等待所有task完成 错误聚合到结果列表里
|
||||||
|
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
|
new_datas = deal_results(new_datas, results)
|
||||||
|
|
||||||
logger.log('INFOR', f'完成异步进行子域的GET请求')
|
logger.log('INFOR', f'完成异步进行子域的GET请求')
|
||||||
return new_datas
|
return new_datas
|
||||||
|
|||||||
Reference in New Issue
Block a user