add nlp test

This commit is contained in:
jiegeaiai 2024-10-13 22:49:17 +08:00
parent ac8a3e7618
commit 9cbeb58f69
2 changed files with 32 additions and 4 deletions

24
test/test_nlp_only.py Normal file
View File

@ -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")

View File

@ -10,19 +10,21 @@ class AsyncTaskQueue:
self._loop = asyncio.new_event_loop() self._loop = asyncio.new_event_loop()
self._thread = threading.Thread(target=self._run_loop) self._thread = threading.Thread(target=self._run_loop)
self._worker_task = None self._worker_task = None
self._loop_running = threading.Event() # self._loop_running = threading.Event()
# self._loop_running.set()
self._thread.start() self._thread.start()
def _run_loop(self): def _run_loop(self):
print('_run_loop') print('_run_loop')
self._loop_running.set()
asyncio.set_event_loop(self._loop) asyncio.set_event_loop(self._loop)
self._loop.run_forever() self._loop.run_forever()
async def _worker(self): async def _worker(self):
print('_worker') print('_worker')
while self._loop_running.is_set(): while True:
print('_worker1')
task = await self._queue.get() task = await self._queue.get()
print('_worker2')
if task is None: if task is None:
break break
print('run task') print('run task')
@ -33,13 +35,15 @@ class AsyncTaskQueue:
def add_task(self, coro): def add_task(self, coro):
print('add_task') print('add_task')
asyncio.run_coroutine_threadsafe(self._queue.put(coro), self._loop) asyncio.run_coroutine_threadsafe(self._queue.put(coro), self._loop)
print('add_task1')
def start_worker(self): def start_worker(self):
print('start_worker')
if not self._worker_task: if not self._worker_task:
self._worker_task = asyncio.run_coroutine_threadsafe(self._worker(), self._loop) self._worker_task = asyncio.run_coroutine_threadsafe(self._worker(), self._loop)
def stop(self): def stop(self):
self._loop_running.clear() # self._loop_running.clear()
asyncio.run_coroutine_threadsafe(self._queue.put(None), self._loop).result() asyncio.run_coroutine_threadsafe(self._queue.put(None), self._loop).result()
if self._worker_task: if self._worker_task:
self._worker_task.result() self._worker_task.result()