【发布时间】:2019-01-24 20:46:02
【问题描述】:
是否可以使用/上传自己的库到 IBM Cloud Functions?还是仅限于预装的软件包?我打算使用 Python 作为编程语言。
【问题讨论】:
标签: python ibm-cloud openwhisk ibm-cloud-functions
是否可以使用/上传自己的库到 IBM Cloud Functions?还是仅限于预装的软件包?我打算使用 Python 作为编程语言。
【问题讨论】:
标签: python ibm-cloud openwhisk ibm-cloud-functions
您可以捆绑自己的依赖项。请参阅此处的文档https://github.com/apache/incubator-openwhisk/blob/master/docs/actions-python.md#packaging-python-actions-with-a-virtual-environment-in-zip-files,以使用您的库创建虚拟环境。文档提供了一个通过 requirements.txt 安装依赖项的示例。
【讨论】:
可以使用比预装库更多的库。有一些tips & tricks in the IBM Cloud Functions docs and the linked blog articles, e.g., here for Python。
对于 Python,您可以使用虚拟环境并将其打包,也可以使用包含所需 Python 文件的 zip 文件。虚拟环境可能更容易开始,但您最终可能会得到很多不必要的文件。我更喜欢下载所需的文件并将它们自己放入一个 zip 文件中。当然,这只能在一定程度上进行管理。
我在IBM Cloud solution tutorial on serverless GitHub traffic statistics 中使用了该方法。你可以找到source code, including the zip file I created for the Python action, in this GitHub repository(见functions folder)。
【讨论】:
您可以使用任何 docker 镜像来执行您的操作,只要这些镜像在 Docker Hub 上可用。因此,您可以使用库创建自己的图像。
因此,例如,如果您想要添加 python 库 yattag 的自己的图像,这是一个从 python 代码生成 HTML 的库。
你可以这样写一个 Dockerfile:
FROM openwhisk/python3action
RUN pip install yattag
然后构建和推送
$ docker build -t docker.io/msciab/python3action-yattag:latest .
$ docker push docker.io/msciab/python3action-yattag
现在您有了一个可以在 OpenWhisk/IBM Cloud 中使用的公共映像。
这是一个使用 yattag 的简单 python hello world:
from yattag import Doc
def main(dict):
doc, tag, text = Doc().tagtext()
with tag('h1'):
text('Hello world!')
dict['body'] = doc.getvalue()
return dict
创建并运行操作:
$ wsk action create hello-yattag hello.py --web true --docker msciab/python3action-yattag
$ curl $(wsk action get hello-yattag --url|tail -1)
<h1>Hello world!</h1>
【讨论】: