再打量并发运行pypeteer时会出现Task was destroyed but it is pending!的问题,发现是因为协程的原因,最好的办法就是在await完成后自动将task任务清除。
async def GetStream(stream_id):
# proxy = 'http://211.127.181.30:40414'
# proxy = 'http://210.196.244.82:53825'
# proxy='http://120.40.214.216:9999'
# proxy = 'http://106.180.225.71:34585'
# headless参数设为False,则变成有头模式
if '--enable-automation' in launcher.DEFAULT_ARGS:
launcher.DEFAULT_ARGS.remove("--enable-automation")
# browser = await launch(
# {'handleSIGINT': False, 'handleSIGTERM': False, 'handleSIGHUP': False},
# headless=False, dumpio=True, userDataDir=r'./text', autoClose=False,
# args=['--proxy-server={}'.format(proxy),'--no-sandbox', '--disable-setuid-sandbox', '--disable-blink-features=AutomationControlled'])
browser = await launch(
{'handleSIGINT': False, 'handleSIGTERM': False, 'handleSIGHUP': False},
headless=False, dumpio=True, userDataDir=r'./text', autoClose=False,
args=['--no-sandbox', '--disable-setuid-sandbox',
'--disable-blink-features=AutomationControlled'])
page = await browser.newPage()
# await page.authenticate({
# 'username': '17475066',
# 'password': '1icmm7xk'
# })
await page.setViewport({
"width": 1068,
"height": 480
})
await page.setUserAgent("Linux/Deepin")
cookies = get_redis('cookies')
await page.setCookie(*cookies)
await page.setRequestInterception(True)
page.on('request', lambda req: asyncio.ensure_future(intercept_request(req, stream_id)))
responseBody = ''
try:
response = await page.goto('https://www.365sb.com/streamingapi/v2/stream')
responseBody = await response.text()
await page.waitFor(2000)
except Exception as e:
await page.close()
await browser.close()
#这里就是在出现异常无法自动关闭task时进行清除
for i in asyncio.Task.all_tasks():
flag = i.cancel()
print('ERROR: ', e)
finally:
await page.close()
await browser.close()
return responseBody
def main(id, servers):
data = {
'state': False, # 结果标记
'vtype': '', # 0: 正常, 1:音视频分离, 2:mpd'
'video': '', # 空
'headers': 'https://www.365sb.com/', # 空
'user_agent': 'Linux/Deepin',
'error_content': ''
}
stream_id = result['stream']
print(stream_id)
lock.acquire()
# 使用cancel方法后仍会出现报错,对其进行错误抛出
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop = asyncio.get_event_loop()
task = loop.create_task(GetStream(stream_id))
loop.run_until_complete(task)
responsebody = task.result()
data = V2stream(responsebody, data)
loop.close()
except:
data['error_content'] = {'code': 101, 'content': '连接被阻止'}
finally:
lock.release()