openfaas / templates

OpenFaaS Classic templates
https://www.openfaas.com
MIT License
276 stars 228 forks source link

Support request for pyperf and Python3 template #276

Closed xueli-daisy closed 2 years ago

xueli-daisy commented 2 years ago

Expected Behaviour

The function should be invoked successfully

Current Behaviour

echo "100"|faas-cli invoke deltablue --gateway http://127.0.0.1:31112 Server returned unexpected status code: 500 - exit status 1 Traceback (most recent call last): File "/home/app/index.py", line 6, in from function import handler File "/home/app/function/handler.py", line 17, in import pyperf ModuleNotFoundError: No module named 'pyperf'

Are you a GitHub Sponsor (Yes/No?)

No

Which Solution Do You Recommend?

Tried with python3-debian template, but I still encounter the same problem

Steps to Reproduce (for bugs)

more deltablue.yml version: 1.0 provider: name: openfaas gateway: http://127.0.0.1:8080 functions: deltablue:

lang: python3

lang: python3
handler: ./deltablue
image: deltablue:latest

limits:
  memory: 256Mi

handler.py is https://github.com/python/pyperformance/blob/main/pyperformance/data-files/benchmarks/bm_deltablue/run_benchmark.py

cat requirements.txt pyperf

Context

Your Environment

alexellis commented 2 years ago

Hi @xueli-daisy unfortunately we cannot support community users on specific python modules.

However, if you take the time to create a GitHub repo with all the code in, I will take a few minutes of my time to build it and see if I can make a recommendation.

By the way, the file you're trying to use as handler.py called run_benchmark.py is not compatible as a function handler file.

For that reason, a full repository would be better and shouldn't take you very long to do.

Please also look into how to format your code or YAML using code blocks in issues:

https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks

Alex

alexellis commented 2 years ago

/add label: support, question

alexellis commented 2 years ago

/set title: Support request for pyperf and Python3 template

alexellis commented 2 years ago

I looked into this with the code example in the pyperf docs: https://pyperf.readthedocs.io/en/latest/run_benchmark.html

version: 1.0
provider:
  name: openfaas
  gateway: http://127.0.0.1:8080
functions:
  pyperf1:
    lang: python3
    handler: ./pyperf1
    image: pyperf1:latest
#!/usr/bin/env python3

import pyperf
import time

def func():
    time.sleep(0.001)

def handle(req):
    """handle a request to the function
    Args:
        req (str): request body
    """

    runner = pyperf.Runner()
    runner.bench_func('sleep', func)

    return req

requirements.txt

pyperf

I found that the same error was created, but only for this particular python module. Others that I tried are working as expected.

It seems to be related to pyperf expecting to be able to hook into __main__.

Unfortunately I can't take this further with free community support.

My suggestion would be for you to try the Dockerfile approach instead, where you can control how pip runs, which exact version of python3 you require and how to set permissions for the libraries. Here's an example of how to write your own Dockerfile: https://www.openfaas.com/blog/openfaas-flask/

If pyperf works in Docker, then it will work on openfaas with this approach.

Alternatively, look into different benchmarking pip modules.

Alex