Python Environment: add gevent based WSGI server framework (#750)
* Python Environment: add gevent based WSGI server framework This supply gevent based WSGI server framework to Python Environment, to allow customer to run their functions under multi-threads. * classify the flask based application This set up the flask based application as individual class, to make sure the consistent and integrity. * Add instruction of WSGI server selection applied in Python Environment * Python Environment starting logger: provide framework name
This commit is contained in:
@@ -43,3 +43,23 @@ Or, if you already have an environment, you can update its image:
|
|||||||
|
|
||||||
After this, fission functions that have the env parameter set to the
|
After this, fission functions that have the env parameter set to the
|
||||||
same environment name as this command will use this environment.
|
same environment name as this command will use this environment.
|
||||||
|
|
||||||
|
## Web Server Framework
|
||||||
|
|
||||||
|
Python environment build and start a WSGI server, to support high HTTP
|
||||||
|
traffic. As it is applied in different use cases, this provides two server
|
||||||
|
frameworks: `bjoern` and `gevent`. They all support high concurrency request.
|
||||||
|
|
||||||
|
`bjoern` has good performance on RPS, to be ideal for most light resource
|
||||||
|
utilization cases.
|
||||||
|
|
||||||
|
`gevent` is a good supplement because of its internal multi-threads. It
|
||||||
|
supports heavy resource load functions, with well distribution of response
|
||||||
|
time.
|
||||||
|
|
||||||
|
Python environment pod remains `bjoern` framework by default. And it runs `gevent`
|
||||||
|
framework by setting the container environment `WSGI_FRAMEWORK` value to `GEVENT`.
|
||||||
|
|
||||||
|
The environment value is configured normally in two ways. One way is to set in Dockerfile
|
||||||
|
and build it into image. The other way is to set in Kubernetes deployment spec during
|
||||||
|
pod running and restart it.
|
||||||
|
|||||||
@@ -5,3 +5,4 @@ python-dateutil
|
|||||||
requests==2.7.0
|
requests==2.7.0
|
||||||
redis
|
redis
|
||||||
hiredis
|
hiredis
|
||||||
|
gevent
|
||||||
|
|||||||
@@ -5,99 +5,107 @@ import sys
|
|||||||
import imp
|
import imp
|
||||||
import os
|
import os
|
||||||
import bjoern
|
import bjoern
|
||||||
|
from gevent.pywsgi import WSGIServer
|
||||||
from flask import Flask, request, abort, g
|
from flask import Flask, request, abort, g
|
||||||
|
|
||||||
app = Flask(__name__)
|
class FuncApp(Flask):
|
||||||
|
def __init__(self, name, loglevel = logging.DEBUG):
|
||||||
|
super(FuncApp, self).__init__(name)
|
||||||
|
|
||||||
userfunc = None
|
# init the class members
|
||||||
|
self.userfunc = None
|
||||||
|
self.root = logging.getLogger()
|
||||||
|
self.ch = logging.StreamHandler(sys.stdout)
|
||||||
|
|
||||||
@app.route('/specialize', methods=['POST'])
|
#
|
||||||
def load():
|
# Logging setup. TODO: Loglevel hard-coded for now. We could allow
|
||||||
global userfunc
|
# functions/routes to override this somehow; or we could create
|
||||||
# load user function from codepath
|
# separate dev vs. prod environments.
|
||||||
codepath = '/userfunc/user'
|
#
|
||||||
userfunc = (imp.load_source('user', codepath)).main
|
self.root.setLevel(loglevel)
|
||||||
return ""
|
self.ch.setLevel(loglevel)
|
||||||
|
self.ch.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
|
||||||
|
self.logger.addHandler(self.ch)
|
||||||
|
|
||||||
@app.route('/v2/specialize', methods=['POST'])
|
#
|
||||||
def loadv2():
|
# Register the routers
|
||||||
global userfunc
|
#
|
||||||
body = request.get_json()
|
@self.route('/specialize', methods=['POST'])
|
||||||
filepath = body['filepath']
|
def load():
|
||||||
handler = body['functionName']
|
# load user function from codepath
|
||||||
|
codepath = '/userfunc/user'
|
||||||
|
self.userfunc = (imp.load_source('user', codepath)).main
|
||||||
|
return ""
|
||||||
|
|
||||||
# The value of "functionName" is consist of `<module-name>.<function-name>`.
|
@self.route('/v2/specialize', methods=['POST'])
|
||||||
moduleName, funcName = handler.split(".")
|
def loadv2():
|
||||||
|
body = request.get_json()
|
||||||
|
filepath = body['filepath']
|
||||||
|
handler = body['functionName']
|
||||||
|
|
||||||
# check whether the destination is a directory or a file
|
# The value of "functionName" is consist of `<module-name>.<function-name>`.
|
||||||
if os.path.isdir(filepath):
|
moduleName, funcName = handler.split(".")
|
||||||
# add package directory path into module search path
|
|
||||||
sys.path.append(filepath)
|
|
||||||
|
|
||||||
# find module from package path we append previously.
|
# check whether the destination is a directory or a file
|
||||||
# Python will try to find module from the same name file under
|
if os.path.isdir(filepath):
|
||||||
# the package directory. If search is successful, the return
|
# add package directory path into module search path
|
||||||
# value is a 3-element tuple; otherwise, an exception "ImportError"
|
sys.path.append(filepath)
|
||||||
# is raised.
|
|
||||||
# Second parameter of find_module enforces python to find same
|
|
||||||
# name module from the given list of directories to prevent name
|
|
||||||
# confliction with built-in modules.
|
|
||||||
f, path, desc = imp.find_module(moduleName, [filepath])
|
|
||||||
|
|
||||||
# load module
|
# find module from package path we append previously.
|
||||||
# Return module object is the load is successful; otherwise,
|
# Python will try to find module from the same name file under
|
||||||
# an exception is raised.
|
# the package directory. If search is successful, the return
|
||||||
try:
|
# value is a 3-element tuple; otherwise, an exception "ImportError"
|
||||||
mod = imp.load_module(moduleName, f, path, desc)
|
# is raised.
|
||||||
finally:
|
# Second parameter of find_module enforces python to find same
|
||||||
if f:
|
# name module from the given list of directories to prevent name
|
||||||
f.close()
|
# confliction with built-in modules.
|
||||||
else:
|
f, path, desc = imp.find_module(moduleName, [filepath])
|
||||||
# load source from destination python file
|
|
||||||
mod = imp.load_source(moduleName, filepath)
|
|
||||||
|
|
||||||
# load user function from module
|
# load module
|
||||||
userfunc = getattr(mod, funcName)
|
# Return module object is the load is successful; otherwise,
|
||||||
|
# an exception is raised.
|
||||||
|
try:
|
||||||
|
mod = imp.load_module(moduleName, f, path, desc)
|
||||||
|
finally:
|
||||||
|
if f:
|
||||||
|
f.close()
|
||||||
|
else:
|
||||||
|
# load source from destination python file
|
||||||
|
mod = imp.load_source(moduleName, filepath)
|
||||||
|
|
||||||
return ""
|
# load user function from module
|
||||||
|
self.userfunc = getattr(mod, funcName)
|
||||||
|
|
||||||
@app.route('/healthz', methods=['GET'])
|
return ""
|
||||||
def healthz():
|
|
||||||
return "", 200
|
|
||||||
|
|
||||||
@app.route('/', methods=['GET', 'POST', 'PUT', 'HEAD', 'OPTIONS', 'DELETE'])
|
@self.route('/healthz', methods=['GET'])
|
||||||
def f():
|
def healthz():
|
||||||
if userfunc == None:
|
return "", 200
|
||||||
print("Generic container: no requests supported")
|
|
||||||
abort(500)
|
|
||||||
#
|
|
||||||
# Customizing the request context
|
|
||||||
#
|
|
||||||
# If you want to pass something to the function, you can add it to 'g':
|
|
||||||
# g.myKey = myValue
|
|
||||||
# And the user func can then access that (after doing a "from flask import g").
|
|
||||||
#
|
|
||||||
return userfunc()
|
|
||||||
|
|
||||||
#
|
@self.route('/', methods=['GET', 'POST', 'PUT', 'HEAD', 'OPTIONS', 'DELETE'])
|
||||||
# Logging setup. TODO: Loglevel hard-coded for now. We could allow
|
def f():
|
||||||
# functions/routes to override this somehow; or we could create
|
if self.userfunc == None:
|
||||||
# separate dev vs. prod environments.
|
print("Generic container: no requests supported")
|
||||||
#
|
abort(500)
|
||||||
def setup_logger(loglevel):
|
#
|
||||||
global app
|
# Customizing the request context
|
||||||
root = logging.getLogger()
|
#
|
||||||
root.setLevel(loglevel)
|
# If you want to pass something to the function, you can add it to 'g':
|
||||||
ch = logging.StreamHandler(sys.stdout)
|
# g.myKey = myValue
|
||||||
ch.setLevel(loglevel)
|
# And the user func can then access that (after doing a "from flask import g").
|
||||||
ch.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
|
#
|
||||||
app.logger.addHandler(ch)
|
return self.userfunc()
|
||||||
|
|
||||||
|
app = FuncApp(__name__, logging.DEBUG)
|
||||||
|
|
||||||
#
|
#
|
||||||
# TODO: this starts the built-in server, which isn't the most
|
# TODO: this starts the built-in server, which isn't the most
|
||||||
# efficient. We should use something better.
|
# efficient. We should use something better.
|
||||||
#
|
#
|
||||||
setup_logger(logging.DEBUG)
|
if os.environ.get("WSGI_FRAMEWORK") == "GEVENT":
|
||||||
app.logger.info("Starting server")
|
app.logger.info("Starting gevent based server")
|
||||||
bjoern.run(app, '0.0.0.0', 8888, reuse_port=True)
|
svc = WSGIServer(('0.0.0.0', 8888), app)
|
||||||
|
svc.serve_forever()
|
||||||
|
else:
|
||||||
|
app.logger.info("Starting bjoern based server")
|
||||||
|
bjoern.run(app, '0.0.0.0', 8888, reuse_port=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user