草庐IT

python - 无服务器 - Numpy - 无法找到好的绑定(bind)路径格式

coder 2024-06-06 原文

我已经为此苦苦挣扎了一个多星期,经历了各种论坛问题和帖子,但无法解决。我正在尝试将 numpy 打包在一个函数中,单独构建需求(我有多个函数和多个我想分开的需求)。

环境:
Windows 10 主页

Windows 版 Docker 工具箱:

Client:
Version:       18.03.0-ce
API version:   1.37
Go version:    go1.9.4
Git commit:    0520e24302


Built: Fri Mar 23 08:31:36 2018
 OS/Arch:       windows/amd64
 Experimental:  false
 Orchestrator:  swarm

Server: Docker Engine - Community
 Engine:
  Version:      18.09.0
  API version:  1.39 (minimum version 1.12)
  Go version:   go1.10.4
  Git commit:   4d60db4
  Built:        Wed Nov  7 00:52:55 2018
  OS/Arch:      linux/amd64
  Experimental: false

无服务器版本:

serverless version 6.4.1
serverless-python-requirements version 6.4.1

目录结构:

|-test
  |-env.yml
  |-serverless.yml
  |-Dockerfile
  |-functions
    |-f1
      |-index.py
      |-requirements.txt
      |-sub_function_1.py
      |-sub_function_2.py
    |-f2
      |-index.py
      |-requirements.txt
      |-sub_function_3.py
      |-sub_function_4.py

serverless.yml

service: test 
plugins:
  - serverless-python-requirements
custom:
  pythonRequirements:
    zip: true                  
    dockerFile: Dockerfile      
    dockerizePip: non-linux     
provider:
  name: aws
  runtime: python3.6
  stage: dev
  environment: ${file(./env.yml):${opt:stage, self:provider.stage}.env}
  region: ${file(./env.yml):${opt:stage, self:provider.stage}.aws.region}
  profile: ${file(./env.yml):${opt:stage, self:provider.stage}.aws.profile}
package:
  individually: true
functions:
  f1:
    handler:index.handler
    module:functions/f1
  f2:
    handler:index.handleer
    module:functions/f2

我的项目文件在 C:\Serverless\test 中。我运行 npm init,然后运行 ​​npm i --save serverless-python-requirements,接受所有默认值。我在 sls deploy -v 上得到以下信息。即使我已将 C:\添加到 VirtualBox 中正在运行的默认 VM 上的共享文件夹,并选择了自动挂载和永久挂载。

如果我注释掉 dockerizePip 和 dockerFile,我会得到以下基于 here 的预期结果和其他 SO 帖子:

 Serverless: Invoke invoke
{
    "errorMessage": "Unable to import module 'index'"
}

如果我注释掉 dockerfile,我会得到:

Serverless: Docker Image: lambci/lambda:build-python3.6

      Error --------------------------------------------------

    error during connect: Get https://XXXXXX/v1.37/version: dial tcp
    XXXXXXXXXX: connectex: A connection attempt failed because the 
    connected party did not properly respond after a period of time, or
    established connection failed because connected host has failed to 
    respond.

    at dockerCommand (C:\Serverless\test\node_modules\serverless-python-requirements\lib\docker.js:20:11)
    at getBindPath (C:\Serverless\test\node_modules\serverless-python-requirements\lib\docker.js:100:3)

使用 Dockerfile

# AWS Lambda execution environment is based on Amazon Linux 1
FROM amazonlinux:1

# Install Python 3.6
RUN yum -y install python36 python36-pip

# Install your dependencies
RUN curl -s https://bootstrap.pypa.io/get-pip.py | python3
RUN yum -y install python3-devel mysql-devel gcc

# Set the same WORKDIR as default image
RUN mkdir /var/task
WORKDIR /var/task

.

Serverless: Building custom docker image from Dockerfile...
Serverless: Docker Image: sls-py-reqs-custom

  Error --------------------------------------------------

  Unable to find good bind path format

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Stack Trace --------------------------------------------

Error: Unable to find good bind path format
    at getBindPath (C:\Serverless\test\node_modules\serverless-python-requirements\lib\docker.js:142:9)
    at installRequirements (C:\Serverless\test\node_modules\serverless-python-requirements\lib\pip.js:152:7)
    at installRequirementsIfNeeded (C:\Serverless\test\node_modules\serverless-python-requirements\lib\pip.js:451:3)

如果我将我的项目移动到 C:\Users\,我会得到这个:

Serverless: Docker Image: sls-py-reqs-custom
Serverless: Trying bindPath /c/Users/Serverless/test/.serverless/requirements (run,--rm,-v,/c/Users/Serverless/test/.serverless/req
uirements:/test,alpine,ls,/test/requirements.txt)
Serverless: /test/requirements.txt

  Error --------------------------------------------------

  docker: Error response from daemon: create "/c/Users/Serverless/test/.serverless/requirements": "\"/c/Users/Serverless/test/.serv
erless/requirements\"" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you in
tended to pass a host directory, use absolute path.
See 'docker run --help'.


     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Stack Trace --------------------------------------------

Error: docker: Error response from daemon: create "/c/Users/Serverless/test/.serverless/requirements": "\"/c/Users/Serverless/test/
.serverless/requirements\"" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If y
ou intended to pass a host directory, use absolute path.
See 'docker run --help'.

    at dockerCommand (C:\Users\Serverless\test\node_modules\serverless-python-requirements\lib\docker.js:20:11)
    at getDockerUid (C:\Users\Serverless\test\node_modules\serverless-python-requirements\lib\docker.js:162:14)

我看过@brianz here 的 Makefile 风格推荐,但我不确定如何对此进行调整(Makefile 不是我的强项)。我对下一步该怎么做有点不知所措,非常感谢您的建议。 TIA。

最佳答案

我无法使插件正常工作,但无论如何我找到了更好的解决方案 - Lambda Layers。这是一个好处,因为它减少了 lambda 的大小并允许代码/文件重用。您可以使用 numpy 和 scipy 的预构建 lambda 层,但我构建了自己的层来向自己展示它是如何工作的。这是我如何让它工作的:

创建图层包:

  1. 打开 EC2 实例或 Ubuntu 或 Linux 或其他任何东西 - 这是必需的,以便我们可以正确编译运行时二进制文件
  2. 制作一个依赖包 zip - 必须使用目录结构 python/lib/python3.6/site-packages 供 python 在运行时查找

    mkdir -p tmpdir/python/lib/python3.6/site-packages 
    pip install -r requirements.txt --no-deps -t tmpdir/python/lib/python3.6/site-packages 
    cd tmpdir zip -r ../py_dependencies.zip . 
    cd .. 
    rm -r tmpdir
    
  3. 将层 zip 推送到 AWS - 需要最新的 awscli

    sudo pip install awscli --upgrade --user
    sudo aws lambda publish-layer-version \
    --layer-name py_dependencies \
    --description "Python 3.6 dependencies [numpy=0.15.4]" \
    --license-info "MIT" \
    --compatible-runtimes python3.6 \
    --zip-file fileb://py_dependencies.zip \
    --profile python_dev_serverless
    
  4. 要在任何需要 numpy 的函数中使用,只需使用在控制台或上面上传期间显示的 arn

    f1:
      handler: index.handler_f_use_numpy
      include:
        - functions/f_use_numpy.py
      layers:
        - arn:aws:lambda:us-west-2:XXXXX:layer:py_dependencies:1
    
  5. 作为额外的好处,您还可以将常量等常见文件推送到层。以下是我在 Windows 和 lambda 上测试使用的方法:

    import platform
    
    \# Set common path
    COMMON_PATH = "../../layers/common/"
    if platform.system() == "Linux": COMMON_PATH = "/opt/common/"
    
    def handler_common(event, context):
        # Read from a constants.json file
        with open(COMMON_PATH + 'constants.json') as f:
            return text = json.load(f)
    

关于python - 无服务器 - Numpy - 无法找到好的绑定(bind)路径格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53936747/

有关python - 无服务器 - Numpy - 无法找到好的绑定(bind)路径格式的更多相关文章

  1. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  2. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  5. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  8. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  9. ruby - ruby 中的 TOPLEVEL_BINDING 是什么? - 2

    它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput

  10. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

随机推荐