|
| 1 | +import asyncio |
| 2 | +import threading |
| 3 | +import time |
| 4 | +import os |
| 5 | +import importlib.resources |
| 6 | + |
| 7 | +from . import KlongInterpreter |
| 8 | +from .utils import CallbackEvent |
| 9 | + |
| 10 | + |
| 11 | +def start_loop(loop: asyncio.AbstractEventLoop, stop_event: asyncio.Event) -> None: |
| 12 | + asyncio.set_event_loop(loop) |
| 13 | + loop.run_until_complete(stop_event.wait()) |
| 14 | + |
| 15 | + |
| 16 | +def setup_async_loop(debug: bool = False, slow_callback_duration: float = 86400.0): |
| 17 | + loop = asyncio.new_event_loop() |
| 18 | + loop.slow_callback_duration = slow_callback_duration |
| 19 | + if debug: |
| 20 | + loop.set_debug(True) |
| 21 | + stop_event = asyncio.Event() |
| 22 | + thread = threading.Thread(target=start_loop, args=(loop, stop_event), daemon=True) |
| 23 | + thread.start() |
| 24 | + return loop, thread, stop_event |
| 25 | + |
| 26 | + |
| 27 | +def cleanup_async_loop(loop: asyncio.AbstractEventLoop, loop_thread: threading.Thread, stop_event: asyncio.Event, debug: bool = False, name: str | None = None) -> None: |
| 28 | + if loop.is_closed(): |
| 29 | + return |
| 30 | + |
| 31 | + loop.call_soon_threadsafe(stop_event.set) |
| 32 | + loop_thread.join() |
| 33 | + |
| 34 | + pending_tasks = asyncio.all_tasks(loop=loop) |
| 35 | + if len(pending_tasks) > 0: |
| 36 | + if name: |
| 37 | + print(f"WARNING: pending tasks in {name} loop") |
| 38 | + for task in pending_tasks: |
| 39 | + loop.call_soon_threadsafe(task.cancel) |
| 40 | + while len(asyncio.all_tasks(loop=loop)) > 0: |
| 41 | + time.sleep(0) |
| 42 | + |
| 43 | + loop.stop() |
| 44 | + |
| 45 | + if not loop.is_closed(): |
| 46 | + loop.close() |
| 47 | + |
| 48 | + |
| 49 | +def append_pkg_resource_path_KLONGPATH() -> None: |
| 50 | + with importlib.resources.as_file(importlib.resources.files('klongpy')) as pkg_path: |
| 51 | + pkg_lib_path = os.path.join(pkg_path, 'lib') |
| 52 | + klongpath = os.environ.get('KLONGPATH', '.:lib') |
| 53 | + klongpath = f"{klongpath}:{pkg_lib_path}" if klongpath else str(pkg_lib_path) |
| 54 | + os.environ['KLONGPATH'] = klongpath |
| 55 | + |
| 56 | + |
| 57 | +def create_repl(debug: bool = False): |
| 58 | + io_loop, io_thread, io_stop = setup_async_loop(debug=debug) |
| 59 | + klong_loop, klong_thread, klong_stop = setup_async_loop(debug=debug) |
| 60 | + |
| 61 | + append_pkg_resource_path_KLONGPATH() |
| 62 | + |
| 63 | + klong = KlongInterpreter() |
| 64 | + shutdown_event = CallbackEvent() |
| 65 | + klong['.system'] = {'ioloop': io_loop, 'klongloop': klong_loop, 'closeEvent': shutdown_event} |
| 66 | + |
| 67 | + return klong, (io_loop, io_thread, io_stop, klong_loop, klong_thread, klong_stop) |
| 68 | + |
| 69 | + |
| 70 | +def cleanup_repl(loops, debug: bool = False) -> None: |
| 71 | + io_loop, io_thread, io_stop, klong_loop, klong_thread, klong_stop = loops |
| 72 | + cleanup_async_loop(io_loop, io_thread, io_stop, debug=debug, name='io_loop') |
| 73 | + cleanup_async_loop(klong_loop, klong_thread, klong_stop, debug=debug, name='klong_loop') |
0 commit comments