kopia lustrzana https://github.com/cirospaciari/socketify.py
better async with run_once hack
rodzic
9b90f5532d
commit
3df4ede94f
|
@ -4,7 +4,6 @@ from .uv import UVLoop
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
|
||||||
def future_handler(future, loop, exception_handler, response):
|
def future_handler(future, loop, exception_handler, response):
|
||||||
try:
|
try:
|
||||||
future.result()
|
future.result()
|
||||||
|
@ -46,43 +45,32 @@ class Loop:
|
||||||
def create_future(self):
|
def create_future(self):
|
||||||
return self.loop.create_future()
|
return self.loop.create_future()
|
||||||
|
|
||||||
def start(self):
|
def keep_alive(self):
|
||||||
self.started = True
|
if self.started:
|
||||||
# run asyncio once per tick
|
self.uv_loop.run_once()
|
||||||
def tick(loop):
|
self.loop.call_soon(self.keep_alive)
|
||||||
# run once asyncio
|
|
||||||
loop.run_once_asyncio()
|
|
||||||
|
|
||||||
# use check for calling asyncio once per tick
|
|
||||||
self.timer = self.uv_loop.create_timer(0, 1, tick, self)
|
|
||||||
# self.timer = self.uv_loop.create_check(tick, self)
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.uv_loop.run()
|
self.started = True
|
||||||
|
while self.started:
|
||||||
|
self.run_once_asyncio()
|
||||||
|
self.uv_loop.run_once()
|
||||||
|
|
||||||
|
# Find all running tasks in main thread:
|
||||||
|
pending = asyncio.all_tasks(self.loop)
|
||||||
|
# Run loop until tasks done
|
||||||
|
self.loop.run_until_complete(asyncio.gather(*pending))
|
||||||
|
|
||||||
def run_once(self):
|
def run_once(self):
|
||||||
self.uv_loop.run_once()
|
self.uv_loop.run_once()
|
||||||
|
|
||||||
def run_once_asyncio(self):
|
def run_once_asyncio(self):
|
||||||
# with suppress(asyncio.CancelledError):
|
|
||||||
# run only one step
|
# run only one step
|
||||||
self.loop.call_soon(self.loop.stop)
|
self.loop._stopping = True
|
||||||
self.loop.run_forever()
|
self.loop._run_once()
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
if self.started:
|
self.started = False
|
||||||
self.timer.stop()
|
|
||||||
self.started = False
|
|
||||||
# unbind run_once
|
|
||||||
# if is still running stops
|
|
||||||
if self.loop.is_running():
|
|
||||||
self.loop.stop()
|
|
||||||
|
|
||||||
self.last_defer = None
|
|
||||||
# Find all running tasks in main thread:
|
|
||||||
pending = asyncio.all_tasks(self.loop)
|
|
||||||
# Run loop until tasks done
|
|
||||||
self.loop.run_until_complete(asyncio.gather(*pending))
|
|
||||||
|
|
||||||
# Exposes native loop for uWS
|
# Exposes native loop for uWS
|
||||||
def get_native_loop(self):
|
def get_native_loop(self):
|
||||||
|
|
|
@ -2508,7 +2508,6 @@ class App:
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
signal.signal(signal.SIGINT, lambda sig, frame: self.close())
|
signal.signal(signal.SIGINT, lambda sig, frame: self.close())
|
||||||
self.loop.start()
|
|
||||||
self.loop.run()
|
self.loop.run()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
|
@ -143,6 +143,9 @@ class UVLoop:
|
||||||
lib.socketify_destroy_loop(self._loop)
|
lib.socketify_destroy_loop(self._loop)
|
||||||
self._handler_data = None
|
self._handler_data = None
|
||||||
|
|
||||||
|
def run_nowait(self):
|
||||||
|
return lib.socketify_loop_run(self._loop, lib.SOCKETIFY_RUN_NOWAIT)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
return lib.socketify_loop_run(self._loop, lib.SOCKETIFY_RUN_DEFAULT)
|
return lib.socketify_loop_run(self._loop, lib.SOCKETIFY_RUN_DEFAULT)
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue