Skip to content

Asynchronous processing

David Salek edited this page Dec 10, 2017 · 18 revisions

The serveless functions in OpenFaaS also work asynchronously.

https://github.com/openfaas/faas/blob/master/guide/asynchronous.md

The advantages of asynchronous processing are:

  • Enable longer time-outs.
  • Process work whenever resources are available rather than immediately.
  • Consume a large batch of work within a few seconds and let it process at its own pace.

The idea of using asynchronous processing for smart security camera is the following:

  • The first serverless function, faas-mobilenet, detects objects in an image and pass the results of the classification together with the image data onto another serverless function.
  • This function, faas-s3-email, uploads the image to Amazon S3 and send an e-mail notification in case a person was detected.

The instructions below show how to achieve this and tie together into a final solution.


Asynchronous invocation

To enable asynchronous processing, run the following script:

docker swarm init
cd faas
./deploy_stack.sh
./deploy_extended.sh

If you want the function to call another function or a different endpoint when it is finished then pass the X-Callback-Url header. You can use the following site to setup a public endpoint for testing HTTP callbacks: https://requestb.in/

faas-cli deploy --image faas-mobilenet --name faas-mobilenet

curl localhost:8080/async-function/faas-mobilenet -d "$(echo -n '{"filename": "50-20171111115421-01.jpg", "image_data": "'; base64 50-20171111115421-01.jpg | tr -d '\n'; echo '"}')" \
-H "X-Callback-Url: https://requestb.in/14sqaoh1"

Asynchronous processing with function calling another function

Deploy the faas-mobilenet function with the output consisting of the list of identified objects and the image showing boxes with these objects:

faas-cli deploy --image faas-mobilenet --name faas-mobilenet --fprocess "python index_output_image.py"

Deploy the faas-s3-email function:

cd faas-s3-email
docker build -t faas-s3-email .
faas-cli deploy --image faas-s3-email --name faas-s3-email
cd ..

Test the latter function:

curl localhost:8080/function/faas-s3-email \
-d "$(echo -n '{"filename": "50-20171111115421-01.jpg", "image_data": "'; base64 50-20171111115421-01.jpg | tr -d '\n'; echo '", "detected_objects": [{"class": "person", "score": 0.9}]}')"

Test asynchronous processing with one function calling the other:

curl 192.168.2.5:8080/async-function/faas-mobilenet \
-d "$(echo -n '{"filename": "50-20171111115421-01.jpg", "image_data": "'; base64 50-20171111115421-01.jpg | tr -d '\n'; echo '"}')" \
-H "X-Callback-Url: http://192.168.2.5:8080/function/faas-s3-email"

The final solution can be deployed by changing the script that gets executed when a new image is saved by the Motion software. Specify /home/pi/rpizero_smart_camera3/process_last_photo.sh in .motion/motion.conf.