run streamlink with asyncio.to_thread
This commit is contained in:
parent
df153d5a12
commit
dd61429524
17
stream.py
17
stream.py
@ -4,6 +4,7 @@ import sys
|
|||||||
import urllib.parse
|
import urllib.parse
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
import base64
|
import base64
|
||||||
import logging
|
import logging
|
||||||
import asyncio
|
import asyncio
|
||||||
@ -98,6 +99,15 @@ class AsyncSession():
|
|||||||
resp = await self.sdata.resp
|
resp = await self.sdata.resp
|
||||||
return AsyncSessionData(resp, self.sdata.current)
|
return AsyncSessionData(resp, self.sdata.current)
|
||||||
|
|
||||||
|
class StreamRunner():
|
||||||
|
def __init__(self, upstream, proxy):
|
||||||
|
self.upstream = upstream
|
||||||
|
self.proxy = proxy
|
||||||
|
def stream(self):
|
||||||
|
return self.proxy.stream().streams(self.upstream)
|
||||||
|
async def run(self):
|
||||||
|
return await asyncio.to_thread(self.stream)
|
||||||
|
|
||||||
proxies = {}
|
proxies = {}
|
||||||
for key in providers:
|
for key in providers:
|
||||||
proxies[key] = []
|
proxies[key] = []
|
||||||
@ -298,6 +308,7 @@ async def rewrite(current, proxy):
|
|||||||
ndata += links.pop(0)
|
ndata += links.pop(0)
|
||||||
ndata += "\n"
|
ndata += "\n"
|
||||||
return ndata
|
return ndata
|
||||||
|
|
||||||
class MainHandler(tornado.web.RequestHandler):
|
class MainHandler(tornado.web.RequestHandler):
|
||||||
async def handle_any(self, redir):
|
async def handle_any(self, redir):
|
||||||
handler = UpstreamHandler()
|
handler = UpstreamHandler()
|
||||||
@ -332,13 +343,17 @@ class MainHandler(tornado.web.RequestHandler):
|
|||||||
async def handle_stream(self, handler, redir):
|
async def handle_stream(self, handler, redir):
|
||||||
upstream = None
|
upstream = None
|
||||||
streams = None
|
streams = None
|
||||||
|
runner = StreamRunner(handler.upstream, handler.proxy)
|
||||||
|
start_time = time.time_ns()
|
||||||
for i in range(5):
|
for i in range(5):
|
||||||
try:
|
try:
|
||||||
streams = handler.proxy.stream().streams(handler.upstream)
|
streams = await runner.run()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.info(e)
|
logger.info(e)
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
stop_time = time.time_ns()
|
||||||
|
logger.info((stop_time - start_time) // 1_000_000)
|
||||||
if streams is not None:
|
if streams is not None:
|
||||||
for key in reversed(streams):
|
for key in reversed(streams):
|
||||||
stream = streams.get(key)
|
stream = streams.get(key)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user