hkj*_*dj1 5 opencv python-3.x aws-lambda serverless-framework
我在 python3.6 中有一个 Lambda 函数,它使用以下包:
opencv-python
imutils
numpy
joblib
mahotas
scikit-image
scikit-learn==0.22.1
sklearn
pymongo==3.10.1
Run Code Online (Sandbox Code Playgroud)
我使用无服务器框架来最小化部署规模并部署到 lambda。我使用该serverless-python-requirements插件来管理包。这是我的 template.yml 文件的样子:
functions:
hello:
handler: handler.hello
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: non-linux
zip: true
slim: true
noDeploy:
- boto3
- botocore
- docutils
- jmespath
- pip
- python-dateutil
- s3transfer
- setuptools
- six
- tensorboard
package:
exclude:
- node_modules/**
- model/**
- .vscode/**
Run Code Online (Sandbox Code Playgroud)
我需要使用slim&zip选项,因为否则部署包将太大(~350mb)。
由于某种原因,如果我不包含pymongoin requirements.txt,该函数运行正常。不包含sls deploy时的输出为:pymongo
Serverless: Adding Python requirements helper...
Serverless: Generated requirements from /home/amman/Desktop/serverless-hello-world/requirements.txt in /home/amman/Desktop/serverless-hello-world/.serverless/requirements.txt...
Serverless: Using static cache of requirements found at /home/amman/.cache/serverless-python-requirements/3967fa669ece2345132bfe2a31be4287e2d61deedfb8b6006997a2192cea5753_slspyc ...
Serverless: Zipping required Python packages...
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Removing Python requirements helper...
Serverless: Injecting required Python packages to package...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service hello-world.zip file to S3 (128.52 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.........
Serverless: Stack update finished...
Run Code Online (Sandbox Code Playgroud)
因此 .zip 总大小约为 128 MB,并且该函数运行良好。但是,如果我包含pymongo,则输出为sls deploy:
Serverless: Adding Python requirements helper...
Serverless: Generated requirements from /home/amman/Desktop/serverless-hello-world/requirements.txt in /home/amman/Desktop/serverless-hello-world/.serverless/requirements.txt...
Serverless: Installing requirements from /home/amman/.cache/serverless-python-requirements/279b0240a975ac6ad3c96e3b0ed81eec7981a8e66e0216037484878bfcaf4479_slspyc/requirements.txt ...
Serverless: Using download cache directory /home/amman/.cache/serverless-python-requirements/downloadCacheslspyc
Serverless: Running ...
Serverless: Zipping required Python packages...
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Removing Python requirements helper...
Serverless: Injecting required Python packages to package...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service hello-world.zip file to S3 (109.37 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.........
Serverless: Stack update finished...
Run Code Online (Sandbox Code Playgroud)
现在大小已减小至约 109 MB。因为我添加了新的依赖项,大小不应该增加吗?当我运行 lambda 函数时,出现错误:
无法导入模块“处理程序”:/tmp/sls-py-req/cv2/cv2.cpython-36m-x86_64-linux-gnu.so:ELF加载命令地址/偏移量未正确对齐
我认为这可能是无服务器框架问题。我可以做什么来解决这个问题?我尝试安装不同版本的pymongo但没有成功。
我正在使用以下无服务器框架版本:
> serverless --version
Framework Core: 1.73.1
Plugin: 3.6.13
SDK: 2.3.1
Components: 2.31.2
Run Code Online (Sandbox Code Playgroud)
编辑: pymongo 有什么替代品吗?我见过一些,但他们使用 pymongo 作为底层依赖项。
我无法用无服务器解决这个问题。所以我决定sls deploy不使用pymongo,一旦无服务器生成.requirements.zip文件,我就将该文件复制到其他地方并再次运行sls deploy,但这次pymongo仅pymongo[srv]在requirements.txt. 生成的.requirements.zip包含 pymongo 及其依赖项。我合并了这个文件和第.requirments.zip一个生成的文件。这样我就可以在一个文件中获得所有其他依赖项(opencv2、numpy、joblib 等)和 pymongo。requirements.zipsls deploy.requirements.zip
之后,我压缩了源代码和合并的.requirements.zip文件,并手动将 zip 上传到 s3。压缩后大小为 128MB。指示我的 lambda 函数使用 S3 中的此部署包,它起作用了。我得到了 pymongo 以及 opencv2 和其他依赖项。
但是,一个缺点是您必须自己上传到S3并更新功能。在这个问题得到解决之前,我将不得不使用这个“hack”。
| 归档时间: |
|
| 查看次数: |
3619 次 |
| 最近记录: |