low energy loop clean up

pull/183/head
Anthony Leung 2024-06-28 08:50:05 +00:00
rodzic e3da9cfa5f
commit 5b44525e8b
1 zmienionych plików z 27 dodań i 69 usunięć

Wyświetl plik

@ -1,6 +1,5 @@
import asyncio
import logging
from time import sleep
import threading
from .tasks import create_task, TaskFactory
from .uv import UVLoop
@ -47,6 +46,7 @@ class Loop:
self.exception_handler = None
self.started = False
self.uv_thread_started = None
if is_pypy: # PyPy async Optimizations
if task_factory_max_items > 0: # Only available in PyPy for now
self._task_factory = TaskFactory(task_factory_max_items)
@ -78,46 +78,18 @@ class Loop:
def create_future(self):
return self.loop.create_future()
def run_uv(self, uv_loop):
uv_loop.run()
#import time
#while True:
# time.sleep(1)
# uv_loop.run_nowait()
def start_uvloop(self):
'''
import time
if not self.started:
time.sleep(1)
self._keep_alive()
'''
if not hasattr(self, 'thread_started'):
logging.info('starting _keep_alive thread')
print('starting _keep_alive thread')
t1 = threading.Thread(target=self.run_uv, daemon=True, args=[self.uv_loop])
def start_uv_loop(self):
if not self.uv_thread_started:
t1 = threading.Thread(target=self.uv_loop.run, daemon=True)
t1.start()
self.thread_started = True
self.uv_thread_started = True
def stop_uv_loop(self):
self.uv_loop.stop()
self.uv_thread_started = False
def _keep_alive(self):
'''if not self.started:
time.sleep(1)
self._keep_alive()
'''
'''
if not hasattr(self, 'thread_started'):
logging.info('starting _keep_alive thread')
t1 = threading.Thread(target=self.run_uv, daemon=True, args=[self.uv_loop])
t1.start()
'''
self.thread_started = True
if self.started:
#sleep(5) # TODO does this still run?
#asyncio.sleep(5)
#logging.info('Commencing self.started loop checking ')
#print('in _k_a')
"""
relax = False
if not self.is_idle:
self._idle_count = 0
@ -125,13 +97,9 @@ class Loop:
self._idle_count += 1
else:
relax = True
#print(self._idle_count, relax)
self.is_idle = True
"""
self.loop.call_later(0.01, self._keep_alive)
return
if relax:
#self.uv_loop.run()
#self.uv_loop.run_nowait()
@ -142,9 +110,6 @@ class Loop:
# be more agressive when needed
self.loop.call_soon(self._keep_alive)
async def slp(self):
await asyncio.sleep(999)
def create_task(self, *args, **kwargs):
# this is not using optimized create_task yet
return self.loop.create_task(*args, **kwargs)
@ -158,14 +123,21 @@ class Loop:
future = self.ensure_future(task)
else:
future = None
print('RUC', flush=True)
# not sure if this method is used. if so,
# might want to use self.start_uvloop() here
# as well?
self.loop.call_soon(self._keep_alive)
"""
this function used by on_start, on_stop handlers
seems shouldnt need the uvloop running yet, otherwise
could start here, the start_uv_loop function is idempotent
self.start_uv_loop()
shouldnt need to wake the loop as run_until_complete ought to
take care of this.
"""
# self.loop.call_soon(self._keep_alive)
#self.wake_asyncio_loop()
self.loop.run_until_complete(future)
#self.wake_asyncio_loop()
# clean up uvloop
#self.uv_loop.stop()
self.stop_uv_loop()
return future
def run(self, task=None):
@ -174,28 +146,14 @@ class Loop:
future = self.ensure_future(task)
else:
future = None
print('RUN1', flush=True)
#self.uv_loop.run()
print('after uvr')
self.start_uvloop()
print('started uvloop', flush=True)
#asyncio.run(self._keep_alive)
#self.loop.call_soon(self._keep_alive)
#self.loop.call_soon(self.slp)
#self.ensure_future(Task(self.slp()))
print('b4 rf')
#await self.slp()
print('after slp')
#l = asyncio.get_event_loop()
#l.run_until_complete(self.slp)
#asyncio.create_task(self.slp)
self.start_uv_loop()
if not self.loop.is_running():
print('run forever')
self.loop.run_forever()
print('after rf')
#asyncio.create_task(self.slp)
# clean up uvloop
"""self.uv_loop.stop()"""
self.stop_uv_loop()
return future
def wake_asyncio_loop(self):