重构ExecutePipeline类以支持异步执行,通过使用asyncio和async def main()函数实现。此更改允许更高效的执行流水线中的任务,并通过异步I/O提高整体性能。
51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
# from src import Plugins
|
|
from src import ExecutePipeline
|
|
from src import EchoProcessor
|
|
from viztracer import VizTracer
|
|
|
|
async def main():
|
|
from pathlib import Path
|
|
|
|
# plug_conf = Plugins(Path(__file__).resolve().parent)
|
|
from src.offical.requests.tongyiqianwen import ChatAiTongYiQianWen
|
|
pipe = ExecutePipeline([
|
|
ChatAiTongYiQianWen(api_key='sk-ab4a3e0e29d54ebaad560c1472933d41', use_stream_api=True),
|
|
EchoProcessor()
|
|
]).start()
|
|
# # plug_conf.load_engine("asr_engine"),
|
|
# # MyEngine(),
|
|
# plug_conf.load_engine("chat_ai_engine"),
|
|
# TeeProcessor(),
|
|
# plug_conf.load_engine("tts_engine"),
|
|
# plug_conf.load_engine("sounds_play_engine"),
|
|
# EchoEngine()
|
|
# exe_input = './tests/offical/sounds/asr_example.wav'
|
|
# exe_input = input('input: ')
|
|
exe_input = '给我一个100字的文章'
|
|
await pipe.write(exe_input)
|
|
await asyncio.sleep(0)
|
|
# loop.run_in_executor(None, inputdata)
|
|
loop = asyncio.get_running_loop()
|
|
def inputdata(pipe, loop):
|
|
while True:
|
|
try:
|
|
data = input('input :')
|
|
asyncio.run_coroutine_threadsafe(pipe.write(data), loop)
|
|
except KeyboardInterrupt:
|
|
pipe.cancel()
|
|
break
|
|
with VizTracer(log_async=True):
|
|
thread = threading.Thread(target=inputdata, args=(pipe,loop,))
|
|
thread.start()
|
|
await pipe.process()
|
|
thread.join()
|
|
# asyncio.gather([i for i in pipe._tasks])
|
|
# await asyncio.to_thread(inputdata, pipe)
|
|
|
|
if __name__ == '__main__':
|
|
from dotenv import load_dotenv
|
|
load_dotenv()
|
|
import asyncio
|
|
import threading
|
|
asyncio.run(main())
|