chainlit 实际部署一些问题

chainlit内部基于了socket.io 进行消息处理,socket.io 是有一些缺陷的,但是也有相关的解决方法,同时
对于启动的入口是加载的一个python 文件,这个处理上是动态加载里边的方法到chainlit 运行环境的

内部一些处理

  • load 模块处理
def load_module(target: str, force_refresh: bool = False):
    """Load the specified module."""
 
    # Get the target's directory
    target_dir = os.path.dirname(os.path.abspath(target))
 
    # Add the target's directory to the Python path
    sys.path.insert(0, target_dir)
 
    if force_refresh:
        # Get current site packages dirs
        site_package_dirs = site.getsitepackages()
 
        # Clear the modules related to the app from sys.modules
        for module_name, module in list(sys.modules.items()):
            if (
                hasattr(module, "__file__")
                and module.__file__
                and module.__file__.startswith(target_dir)
                and not any(module.__file__.startswith(p) for p in site_package_dirs)
            ):
                sys.modules.pop(module_name, None)
 
    spec = util.spec_from_file_location(target, target)
    if not spec or not spec.loader:
        return
 
    module = util.module_from_spec(spec)
    if not module:
        return
 
    spec.loader.exec_module(module)
 
    sys.modules[target] = module
 
    # Remove the target's directory from the Python path
    sys.path.pop(0)
  • server 启动
    server的启动是基于fastapi + uvicorn 的
async def start():
    config = uvicorn.Config(
        app,
        host=host,
        port=port,
        ws=ws_protocol,
        log_level=log_level,
        ws_per_message_deflate=ws_per_message_deflate,
        ssl_keyfile=ssl_keyfile,
        ssl_certfile=ssl_certfile,
    )
    server = uvicorn.Server(config)
    await server.serve()
# Run the asyncio event loop instead of uvloop to enable re entrance
asyncio.run(start())

一些问题

目前对于uvicorn 如果需要开启多worker 需要使用 string 的模式传递应用,所以目前的模式是不能支持多worker的,可以调整,但是因为上边动态加载python 代码的问题,造成worker对于chainlit 应用加载会有问题,修改之后 ,因为多worker 运行 ,socket.io 会存在session 不一致的问题,应该基于sticky-session 解决方法,socket.io的adapter 是一种解决方法,但是是多server
消息广播的

说明

目前来说对于chainlit 实际部署最好的模式就是多实例(不是多worker),然后基于lb进行负载均衡,同时注意session共享的问题

参考资料

https://github.com/Chainlit/chainlit/issues/719
https://python-socketio.readthedocs.io/en/stable/server.html#user-sessions
https://socket.io/docs/v4/using-multiple-nodes/#why-is-sticky-session-required
https://socket.io/docs/v4/adapter/

posted on   荣锋亮  阅读(164)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-10-13 cube.js 最新版本特性
2019-10-13 ent 基本使用 一 schema 迁移
2019-10-13 ent facebook 开源的golang orm 框架
2019-10-13 fastify nodejs框架
2019-10-13 blessed-contrib 开发终端dashboard 的几点说明
2018-10-13 flynn 开源paas 平台安装试用
2016-10-13 maven 几个插件的使用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示