V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Wait845
V2EX  ›  问与答

Python 的 websockets 用的是 async,是不是意味着如果用这个做服务端的话,所有功能都得用 asyncio 写?

  •  
  •   Wait845 · 2021-12-10 16:45:25 +08:00 · 794 次点击
    这是一个创建于 839 天前的主题,其中的信息可能已经有所发展或是发生改变。
    4 条回复    2021-12-10 17:01:45 +08:00
    ipwx
        1
    ipwx  
       2021-12-10 16:49:59 +08:00   ❤️ 1
    1. 对于不能 asyncio 的部分你可以用 loop.run_in_executor() 转换到线程池上做,不过……
    2. 很多数据库都有 asyncio 版。比如 aiopg (postgres), aiomysql (mysql), motor (mongodb)
    3. 文件交互可以用 aiofiles ,如果 service static file to http 直接丢给比如 starlette 。
    4. 其实 asyncio 写起来很爽。
    tigerstudent
        2
    tigerstudent  
       2021-12-10 16:54:25 +08:00   ❤️ 1
    vicalloy
        3
    vicalloy  
       2021-12-10 16:57:17 +08:00   ❤️ 1
    你可以参考一下 https://github.com/zulip/zulip 这个项目。
    websocket 相关操作用 tornado ,web 部分用 django 。
    Buges
        4
    Buges  
       2021-12-10 17:01:45 +08:00 via Android   ❤️ 1
    python 的 async 和 rust 十分接近,所以用起来比较费劲,但也因而容易控制,只需要把 async 的部分放到 async executor 上跑,其他的还是按同步写就行。
    另外 trio 值得尝试。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5881 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:28 · PVG 10:28 · LAX 19:28 · JFK 22:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.