kopia lustrzana https://github.com/cirospaciari/socketify.py
add idle config
rodzic
833f07b711
commit
70199e8d7d
|
@ -25,12 +25,13 @@ async def task_wrapper(exception_handler, loop, response, task):
|
|||
|
||||
|
||||
class Loop:
|
||||
def __init__(self, exception_handler=None, task_factory_max_items=0):
|
||||
def __init__(self, exception_handler=None, task_factory_max_items=0, idle_relaxation_time=0.01):
|
||||
|
||||
# get the current running loop or create a new one without warnings
|
||||
self.loop = asyncio._get_running_loop()
|
||||
self._idle_count = 0
|
||||
self.is_idle = False
|
||||
self.idle_relaxation_time = idle_relaxation_time
|
||||
if self.loop is None:
|
||||
self.loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(self.loop)
|
||||
|
@ -97,7 +98,7 @@ class Loop:
|
|||
self.loop.call_later(0.001, self._keep_alive)
|
||||
else:
|
||||
# we are really idle now lets use less CPU
|
||||
self.loop.call_later(0.01, self._keep_alive)
|
||||
self.loop.call_later(self.idle_relaxation_time, self._keep_alive)
|
||||
else:
|
||||
self.uv_loop.run_nowait()
|
||||
# be more agressive when needed
|
||||
|
@ -109,6 +110,11 @@ class Loop:
|
|||
|
||||
def ensure_future(self, task):
|
||||
return asyncio.ensure_future(task, loop=self.loop)
|
||||
|
||||
def create_background_task(self, bg_task):
|
||||
def next_tick():
|
||||
self.ensure_future(bg_task())
|
||||
self.loop.call_soon(next_tick)
|
||||
|
||||
def run_until_complete(self, task=None):
|
||||
self.started = True
|
||||
|
@ -121,12 +127,7 @@ class Loop:
|
|||
# clean up uvloop
|
||||
self.uv_loop.stop()
|
||||
return future
|
||||
|
||||
def create_background_task(self, bg_task):
|
||||
def next_tick():
|
||||
self.ensure_future(bg_task())
|
||||
self.loop.call_soon(next_tick)
|
||||
|
||||
|
||||
def run(self, task=None):
|
||||
self.started = True
|
||||
if task is not None:
|
||||
|
|
|
@ -2599,6 +2599,7 @@ class App:
|
|||
websocket_factory_max_items=0,
|
||||
task_factory_max_items=100_000,
|
||||
lifespan=True,
|
||||
idle_relaxation_time=0,
|
||||
):
|
||||
|
||||
socket_options_ptr = ffi.new("struct us_socket_context_options_t *")
|
||||
|
@ -2677,6 +2678,7 @@ class App:
|
|||
self.loop = Loop(
|
||||
lambda loop, context, response: self.trigger_error(context, response, None),
|
||||
task_factory_max_items,
|
||||
idle_relaxation_time,
|
||||
)
|
||||
self.run_async = self.loop.run_async
|
||||
# set async loop to be the last created (is thread_local), App must be one per thread otherwise will use only the lasted loop
|
||||
|
|
Ładowanie…
Reference in New Issue