From 9cbeb58f699c1b59d085a0266d304260000a0b7b Mon Sep 17 00:00:00 2001 From: jiegeaiai Date: Sun, 13 Oct 2024 22:49:17 +0800 Subject: [PATCH] add nlp test --- test/test_nlp_only.py | 24 ++++++++++++++++++++++++ utils/async_task_queue.py | 12 ++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 test/test_nlp_only.py diff --git a/test/test_nlp_only.py b/test/test_nlp_only.py new file mode 100644 index 0000000..e07a7e4 --- /dev/null +++ b/test/test_nlp_only.py @@ -0,0 +1,24 @@ +#encoding = utf8 + +import time + +from nlp import PunctuationSplit, DouBao + + +def main(): + print("Started! Please speak") + split = PunctuationSplit() + nlp = DouBao(split) + nlp.ask('你好') + nlp.ask('你是谁') + nlp.ask('能做什么') + time.sleep(20) + print("Stop! ") + nlp.stop() + + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + print("\nCaught Ctrl + C. Exiting") diff --git a/utils/async_task_queue.py b/utils/async_task_queue.py index fefb958..0dfa505 100644 --- a/utils/async_task_queue.py +++ b/utils/async_task_queue.py @@ -10,19 +10,21 @@ class AsyncTaskQueue: self._loop = asyncio.new_event_loop() self._thread = threading.Thread(target=self._run_loop) self._worker_task = None - self._loop_running = threading.Event() + # self._loop_running = threading.Event() + # self._loop_running.set() self._thread.start() def _run_loop(self): print('_run_loop') - self._loop_running.set() asyncio.set_event_loop(self._loop) self._loop.run_forever() async def _worker(self): print('_worker') - while self._loop_running.is_set(): + while True: + print('_worker1') task = await self._queue.get() + print('_worker2') if task is None: break print('run task') @@ -33,13 +35,15 @@ class AsyncTaskQueue: def add_task(self, coro): print('add_task') asyncio.run_coroutine_threadsafe(self._queue.put(coro), self._loop) + print('add_task1') def start_worker(self): + print('start_worker') if not self._worker_task: self._worker_task = asyncio.run_coroutine_threadsafe(self._worker(), self._loop) def stop(self): - self._loop_running.clear() + # self._loop_running.clear() asyncio.run_coroutine_threadsafe(self._queue.put(None), self._loop).result() if self._worker_task: self._worker_task.result()