Try running a function written in Python using Fn Project

Overview

This entry uses the OSS FaaS server "Fn Project" to handle patterns that run Python functions.

Deploy and run the multiplication function by Python as shown below on the Fn server.

calcfn_multiply.png

Assumed reader

--Python program in Fn Project Those who haven't run it yet -"Try running a function written in JavaScript using Fn Project with Node" Those who are interested in the Python version

Preparation

Please refer to another entry "Try installing Fn Project server on CentOS with Micro Instance of OCI" until you run Fn Project.

In this entry, I'm working on an OCI Micro Instance.

Run function in python

The basic flow of work follows what is written in Introduction to Fn with Python.

App on Fn

In another entry "Try running a function written in JavaScript using Fn Project with Node", with "fn create app", on fn It takes the form of adding a Function to the "calc-fn-app" application that has been created.

Registration

Execute "fn init" to create one function in Python at the position / multiply.

$ fn init --runtime python --trigger http multiply
Creating function at: ./multiply
Function boilerplate generated.
func.yaml created.

The following three files are created.

$ ls
func.py  func.yaml  requirements.txt

Change code

Rewrite it as a function that returns by multiplying the input value left and right as shown below.

import io
import json
from decimal import *

from fdk import response


def handler(ctx, data: io.BytesIO=None):
    value = Decimal('NaN')
    try:
        body = json.loads(data.getvalue())
        left = Decimal(body.get('left'))
        right = Decimal(body.get('right'))
        value = left * right
    except (Exception, ValueError) as ex:
        print(str(ex))

    return response.Response(
        ctx, response_data=json.dumps(
            {"result": str(value)}),
        headers={"Content-Type": "application/json"}
    )

Deploy the app. The working directory is specified with "-w" and registered.

fn --verbose deploy --app calc-fn-app --local -w /home/opc/calc-fn-app/multiply

At the first execution, it seems that the flow is as follows.

$ fn --verbose deploy --app calc-fn-app --local -w /home/opc/calc-fn-app/multiply
Deploying multiply to app: calc-fn-app
Bumped to version 0.0.2
Building image fndemouser/multiply:0.0.2
FN_REGISTRY:  fndemouser
Current Context:  default
Sending build context to Docker daemon  6.144kB
Step 1/12 : FROM fnproject/python:3.6-dev as build-stage
3.6-dev: Pulling from fnproject/python
80369df48736: Pull complete
aaba0609d543: Pull complete
a97b990f94a5: Pull complete
af4a941e5376: Pull complete
709c35256bb6: Pull complete
3c3deb8445b4: Pull complete
Digest: sha256:8ac8c28a68fd0442b9ddcdf6a41f30230482d72d1024cafca06c9f1ac0bd821c
Status: Downloaded newer image for fnproject/python:3.6-dev
 ---> c5dbe9a0175b
Step 2/12 : WORKDIR /function
 ---> Running in f7aaa0e58f7a
Removing intermediate container f7aaa0e58f7a
 ---> 697f68e69e7c
Step 3/12 : ADD requirements.txt /function/
 ---> c4472f73a275
Step 4/12 : RUN pip3 install --target /python/  --no-cache --no-cache-dir -r requirements.txt &&                         rm -fr ~/.cache/pip /tmp* requirements.txt func.yaml Dockerfile .venv
 ---> Running in 4f758b74080b
Collecting fdk
  Downloading https://files.pythonhosted.org/packages/1d/b8/41b81bf76766f7e810627728647a8076626070a6e1d01a18a8ed16bd3d3f/fdk-0.1.12-py3-none-any.whl (46kB)
Collecting iso8601==0.1.12
  Downloading https://files.pythonhosted.org/packages/ef/57/7162609dab394d38bbc7077b7ba0a6f10fb09d8b7701ea56fa1edc0c4345/iso8601-0.1.12-py2.py3-none-any.whl
Collecting httptools>=0.0.10
  Downloading https://files.pythonhosted.org/packages/1b/03/215969db11abe8741e9c266a4cbe803a372bd86dd35fa0084c4df6d4bd00/httptools-0.0.13.tar.gz (104kB)
Collecting pbr!=2.1.0,>=2.0.0
  Downloading https://files.pythonhosted.org/packages/7a/db/a968fd7beb9fe06901c1841cb25c9ccb666ca1b9a19b114d1bbedf1126fc/pbr-5.4.4-py2.py3-none-any.whl (110kB)
Collecting pytest==4.0.1
  Downloading https://files.pythonhosted.org/packages/81/27/d4302e4e00497448081120f65029696070806bc8e649b83f644de006d710/pytest-4.0.1-py2.py3-none-any.whl (217kB)
Collecting pytest-asyncio==0.9.0
  Downloading https://files.pythonhosted.org/packages/33/7f/2ed9f460872ebcc62d30afad167673ca10df36ff56a6f6df2f1d3671adc8/pytest_asyncio-0.9.0-py3-none-any.whl
Collecting py>=1.5.0
  Downloading https://files.pythonhosted.org/packages/99/8d/21e1767c009211a62a8e3067280bfce76e89c9f876180308515942304d2d/py-1.8.1-py2.py3-none-any.whl (83kB)
Collecting pluggy>=0.7
  Downloading https://files.pythonhosted.org/packages/a0/28/85c7aa31b80d150b772fbe4a229487bc6644da9ccb7e427dd8cc60cb8a62/pluggy-0.13.1-py2.py3-none-any.whl
Collecting attrs>=17.4.0
  Downloading https://files.pythonhosted.org/packages/a2/db/4313ab3be961f7a763066401fb77f7748373b6094076ae2bda2806988af6/attrs-19.3.0-py2.py3-none-any.whl
Collecting setuptools
  Downloading https://files.pythonhosted.org/packages/a7/c5/6c1acea1b4ea88b86b03280f3fde1efa04fefecd4e7d2af13e602661cde4/setuptools-45.1.0-py3-none-any.whl (583kB)
Collecting more-itertools>=4.0.0
  Downloading https://files.pythonhosted.org/packages/72/96/4297306cc270eef1e3461da034a3bebe7c84eff052326b130824e98fc3fb/more_itertools-8.2.0-py3-none-any.whl (43kB)
Collecting six>=1.10.0
  Downloading https://files.pythonhosted.org/packages/65/eb/1f97cb97bfc2390a276969c6fae16075da282f5058082d4cb10c6c5c1dba/six-1.14.0-py2.py3-none-any.whl
Collecting atomicwrites>=1.0
  Downloading https://files.pythonhosted.org/packages/52/90/6155aa926f43f2b2a22b01be7241be3bfd1ceaf7d0b3267213e8127d41f4/atomicwrites-1.3.0-py2.py3-none-any.whl
Collecting importlib-metadata>=0.12; python_version < "3.8"
  Downloading https://files.pythonhosted.org/packages/8b/03/a00d504808808912751e64ccf414be53c29cad620e3de2421135fcae3025/importlib_metadata-1.5.0-py2.py3-none-any.whl
Collecting zipp>=0.5
  Downloading https://files.pythonhosted.org/packages/be/69/4ac28bf238f287f1677f41392e24d2c4ffafcf11648c23824f5f62ef6ccb/zipp-2.1.0-py3-none-any.whl
Building wheels for collected packages: httptools
  Building wheel for httptools (setup.py): started
  Building wheel for httptools (setup.py): finished with status 'done'
  Created wheel for httptools: filename=httptools-0.0.13-cp36-cp36m-linux_x86_64.whl size=217310 sha256=51eb19168c0639662416a8ad83b9df7d7ceb8eb14a58ce3bec5ffe3ab3b07a7f
  Stored in directory: /tmp/pip-ephem-wheel-cache-pwxrmfa5/wheels/e8/3e/2e/013f99b42efc25cf3589730cf380738e46b1e5edaf2f78d525
Successfully built httptools
Installing collected packages: iso8601, httptools, pbr, py, zipp, importlib-metadata, pluggy, attrs, setuptools, more-itertools, six, atomicwrites, pytest, pytest-asyncio, fdk
Successfully installed atomicwrites-1.3.0 attrs-19.3.0 fdk-0.1.12 httptools-0.0.13 importlib-metadata-1.5.0 iso8601-0.1.12 more-itertools-8.2.0 pbr-5.4.4 pluggy-0.13.1 py-1.8.1 pytest-4.0.1 pytest-asyncio-0.9.0 setuptools-45.1.0 six-1.14.0 zipp-2.1.0
WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container 4f758b74080b
 ---> 5246c18d4e6f
Step 5/12 : ADD . /function/
 ---> c6181bb33bd1
Step 6/12 : RUN rm -fr /function/.pip_cache
 ---> Running in 695fe059e692
Removing intermediate container 695fe059e692
 ---> 5ea69eaafc3a
Step 7/12 : FROM fnproject/python:3.6
3.6: Pulling from fnproject/python
80369df48736: Already exists
aaba0609d543: Already exists
a97b990f94a5: Already exists
af4a941e5376: Already exists
709c35256bb6: Already exists
671870542c6c: Pull complete
936a6f40830a: Pull complete
Digest: sha256:3b438ba11405bba0f6e1e8d8819c9b8be38249da7253491f3cf9f7e5ed6c0ec6
Status: Downloaded newer image for fnproject/python:3.6
 ---> e8e10863d7cd
Step 8/12 : WORKDIR /function
 ---> Running in ddbc794f4bd9
Removing intermediate container ddbc794f4bd9
 ---> 4e3bed62a7f5
Step 9/12 : COPY --from=build-stage /python /python
 ---> b1c7d4fd7598
Step 10/12 : COPY --from=build-stage /function /function
 ---> 87446e069bd2
Step 11/12 : ENV PYTHONPATH=/function:/python
 ---> Running in af075fbcae2c
Removing intermediate container af075fbcae2c
 ---> 98a9f515e7e0
Step 12/12 : ENTRYPOINT ["/python/bin/fdk", "/function/func.py", "handler"]
 ---> Running in 555033ab1e55
Removing intermediate container 555033ab1e55
 ---> 9942a3d98858
Successfully built 9942a3d98858
Successfully tagged fndemouser/multiply:0.0.2

Updating function multiply using image fndemouser/multiply:0.0.2...
Successfully created function: multiply with fndemouser/multiply:0.0.2
Successfully created trigger: multiply
Trigger Endpoint: http://127.0.0.1:18080/t/calc-fn-app/multiply

Check with the fn command. (Plus, minus are made in another entry)

$ fn list fn calc-fn-app
NAME            IMAGE                           ID
multiply        fndemouser/multiply:0.0.2       01E086F2D6NG8G00GZJ000001E
plus            fndemouser/plus:0.0.2           01E0393FDCNG8G00GZJ000000E
subtract        fndemouser/subtract:0.0.3       01E05KAKGENG8G00GZJ0000013

If you check with the Docker command, you can see that the image of the corresponding container is created.

$ docker images | grep multiply
fndemouser/multiply   0.0.2               9942a3d98858        About a minute ago   175MB

Run

Try accessing the URL at the end of registration with curl.

$ curl -d '{"left":"2", "right":"4"}' http://127.0.0.1:18080/t/calc-fn-app/multiply
{"result": "8"}

8 which is the result of 2 * 4 is returned.

in conclusion

In this entry, I dealt with trying to run a function written in Python using Fn project.

The code used in this entry is [https://github.com/hrkt/calc-fn-app/releases/tag/0.0.3](https://github.com/hrkt/calc-fn-app/ It is stored in the tag of releases / tag / 0.0.3).

Supplement: External library

I didn't use an external library in this entry, but you can use pip by listing it in requirements.txt.

Note: In writing this entry

In writing this entry, I created the following situation on a micro instance of OCI and worked by remotely connecting it from a laptop to the cloud side with ssh.

--Work by raising multiple windows with screen before ssh --Run VS Code on the WEB "Try running VS Code on a Micro instance of always Free of cdr / code-server and OCI" --Run the Fn project server there too --Code on GitHub

Recommended Posts

Try running a function written in Python using Fn Project
Create a function in Python
[Unity (C #), Python] Try running Python code in Unity using IronPython
Try using LevelDB in Python (plyvel)
To return char * in a callback function using ctypes in Python
Try building a neural network in Python without using a library
A memo of writing a basic function in Python using recursion
Try using Leap Motion in Python
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Precautions when pickling a function in python
Try sending a SYN packet in Python
Try drawing a simple animation in Python
Scraping a website using JavaScript in Python
Draw a tree in Python 3 using graphviz
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
Draw a graph of a quadratic function in Python
Create a GIF file using Pillow in Python
To execute a Python enumerate function in JavaScript
Get the caller of a function in Python
View drug reviews using a list in Python
Create a MIDI file in Python using pretty_midi
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
Try gRPC in Python
Try 9 slices in Python
Try using Tweepy [Python2.7]
Parse a JSON string written to a file in Python
Create a data collection bot in Python using Selenium
Try to make a Python module in C language
Try creating a compressed file using Python and zlib
(Python) Try to develop a web application using Django
Try searching for a million character profile in Python
Try embedding Python in a C ++ program with pybind11
What's in that variable (when running a Python script)
Try face detection in real time using a webcam
[Python] I tried running a local server using flask
What does the last () in a function mean in Python?
How to unit test a function containing the current time using freezegun in python
Take a screenshot in Python
Try to make it using GUI and PyQt in Python
To receive multiple return values ​​from a function executed using parallel processing (Pool) in Python
Try creating a Deep Zoom file format .DZI in Python
[Python] Try using Tkinter's canvas
Use callback function in Python
Try function optimization using Hyperopt
ntile (decile) function in python
Try using Kubernetes Client -Python-
Try a similar search for Image Search using the Python SDK [Search]
Try creating a CRUD function
A memo when creating a directed graph using Graphviz in Python
Try LINE Notify in Python
Draw implicit function in python
[python] Manage functions in a dictionary (command table, function table, function pointer)
Try running Python with Try Jupyter
Try implementing Yubaba in Python 3
Immediate function in python (lie)
Gacha written in Python -BOX gacha-
I tried to make a stopwatch using tkinter in python
Draw a heart in Python
Wrapper running Hadoop in Python