跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Python] 简单改造了一下阿里云的 oss2 模块,底层改用 httpx,支持异步和类型提示

发表于

项目代码: https://github.com/AndersonBY/aliyun-oss-x

起因是我的一些服务跑在阿里云的函数计算上,最近感觉冷启动实在是太慢了,查了一下发现阿里云官方的 oss2 模块的导入实在是相当耗时,我用最低配的函数计算服务冷启动大概需要花 5 秒才能导入完 oss2 。而我大部分的函数计算实际上只需要简单的文件上传和下载,所以打算自己改写一下官方的 oss2 模块,于是就有了这个 aliyun-oss-x 模块。

我大概花了一天多的时间把 oss2 项目简单重构了一下,底层从 requests 改成了 httpx ,所以可以支持异步方法的使用了,而且我花了不少时间把尽量能懒加载的模块都延迟加载了,以及把类型标注给补上了一些。

简单说一下我看到 oss2 的一些问题。

  1. 默认安装并导入了 aliyunsdkkms 和 aliyunsdkcore ,这两个模块是启动耗时大户之一。然而只有使用阿里云的 KMS 服务才需要用到,所以没必要放顶层导入。
  2. 基本没有类型标注,导致很多方法的返回值都是 Unknown ,没法直接让 IDE 获取返回值的属性。
  3. 底层用的 requests ,没有异步支持。
  4. models 和 api 文件都是所有东西放在一个单一文件里,都超过 3000 行,导致重构略微麻烦。

目前 1~3 都解决了,4 的话要花很多时间暂时没做太多调整。我简单测了基本的一些对象操作(同步和异步)都没有问题,但是可能有不太常用的功能会有问题,暂时没有完整测试过。

另外我简单测试了一下用新模块之后函数计算的冷启动少了一半时间,而且内存占用也从 70 M 降到了 40 M 。至少冷启动这里的耗时降低能直接给我省钱了。

欢迎有兴趣的朋友可以试试看,有问题的话随时提 issue 。

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.