chainlit 异步以及同步转换处理简单说明
chainlit 提供了工具方法,可以方便的将同步转异步,同时将异步转同步
使用
- 同步转异步
from chainlit import make_async
def my_sync_function():
# Your synchronous code goes here
import time
time.sleep(10)
return 0
async_function = make_async(my_sync_function)
async def main():
result = await async_function()
- 异步转同步
from chainlit import run_sync
async def my_async_function():
# Your asynchronous code goes here
def main():
result = run_sync(my_async_function())
main()
内部处理
内部处理上chainlit使用了syncer以及asyncer 这几个包asyncer,是直接使用的,对于异步转同步,基于上下文进行了判断处理
- 参考处理
import sys
from typing import Any, Coroutine, TypeVar
if sys.version_info >= (3, 10):
from typing import ParamSpec
else:
from typing_extensions import ParamSpec
import asyncio
import threading
from asyncer import asyncify
from chainlit.context import context_var
from syncer import sync
make_async = asyncify
T_Retval = TypeVar("T_Retval")
T_ParamSpec = ParamSpec("T_ParamSpec")
T = TypeVar("T")
def run_sync(co: Coroutine[Any, Any, T_Retval]) -> T_Retval:
"""Run the coroutine synchronously."""
# Copy the current context
current_context = context_var.get()
# Define a wrapper coroutine that sets the context before running the original coroutine
async def context_preserving_coroutine():
# Set the copied context to the coroutine
context_var.set(current_context)
return await co
# Execute from the main thread in the main event loop
if threading.current_thread() == threading.main_thread():
return sync(context_preserving_coroutine())
else: # Execute from a thread in the main event loop
result = asyncio.run_coroutine_threadsafe(
context_preserving_coroutine(), loop=current_context.loop
)
return result.result()
参考资料
https://docs.chainlit.io/guides/sync-async
https://github.com/miyakogi/syncer
https://github.com/fastapi/asyncer
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2022-08-31 主机网络限速+测速工具
2022-08-31 nginx proxy_pass 包含路径问题
2022-08-31 windows jenkins openssh 集成问题
2020-08-31 orika java bean 转换工具试用
2020-08-31 graalvm js 内置commonjs 模式试用
2020-08-31 graalvm js 与java 类型转换的一些方法
2019-08-31 haproxy 2.0 dataplaneapi docker 镜像