我正在尝试在 AWS Lambda 上部署一个 Python 应用程序。它有几个较大的 Python 依赖项,其中最大的是 scipy 和 numpy。结果我的应用程序比允许的 250MB 大得多。
在尝试找到一种方法来减小大小时,我发现了这里详细介绍的方法:
https://github.com/szelenka/shrink-linalg
实质上,在使用 pip 安装时,可以通过向 c 编译器传递标志,在 scipy 和 numpy 的 Cython 编译期间省略已编译的 c 二进制文件中的调试信息。结果是 scipy 和 numpy 的大小缩小了约 50%。我能够在本地(ubuntu 16.04)运行它,并且没有任何问题地创建了二进制文件。使用的命令是:
CFLAGS="-g0 -I/usr/include:/usr/local/include -L/usr/lib:/usr/local/lib" pip install numpy scipy --compile --no-cache-dir --global-option=build_ext --global-option="-j 4"
问题在于为了在 AWS Lambda 上运行,二进制文件必须在与 Lambda 运行环境类似的环境中进行编译。该环境的镜像可以在此处找到:
https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
在将镜像加载到 EC2 实例后,在安装了一些依赖项后尝试运行相同的 pip 安装。
sudo yum install python36 python3-devel blas-devel atlas atlas-devel lapack-devel atlas-sse3-devel gcc gcc-64 gcc-gfortran gcc64-gfortran libgfortran, gcc-c++ openblas-devel python36-virtualenv
NumPy编译正常,但Scipy没有。Cython没有引起任何问题,但Fortran编译出现了以下错误:
error: Command "/usr/bin/gfortran -Wall -g -Wall -g -shared build/temp.linux-x86_64-3.6/build/src.linux-x86_64-3.6/scipy/integrate/_test_odeint_bandedmodule.o build/temp.linux-x86_64-3.6/build/src.linux-x86_64-3.6/build/src.linux-x86_64-3.6/scipy/integrate/fortranobject.o build/temp.linux-x86_64-3.6/scipy/integrate/tests/banded5x5.o build/temp.linux-x86_64-3.6/build/src.linux-x86_64-3.6/scipy/integrate/_test_odeint_banded-f2pywrappers.o -L/usr/lib64/atlas -L/usr/lib/gcc/x86_64-amazon-linux/6.4.1 -L/usr/lib/gcc/x86_64-amazon-linux/6.4.1 -L/usr/lib64 -Lbuild/temp.linux-x86_64-3.6 -llsoda -lmach -llapack -lptf77blas -lptcblas -latlas -lptf77blas -lptcblas -lpython3.6m -lgfortran -o build/lib.linux-x86_64-3.6/scipy/integrate/_test_odeint_banded.cpython-36m-x86_64-linux-gnu.so -Wl,--version-script=build/temp.linux-x86_64-3.6/link-version-scipy.integrate._test_odeint_banded.map" failed with exit status 1
我已经尝试重新安装gfortran以及整个gcc集合,但都没有什么好运。不幸的是,我对Fortran编译器的经验非常有限。如果有人有任何想法或者已经编译好了c二进制文件的版本,我会非常感激。