Skip to content

Commit

Permalink
gpu
Browse files Browse the repository at this point in the history
  • Loading branch information
guokan-shang committed Apr 12, 2023
1 parent 61a8928 commit 5a1b5d3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions .envdefault
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ LM_MAP = "{
# SERVING PARAMETERS
SERVICE_MODE=http
CONCURRENCY=1
USE_GPU=True

# MICRO-SERVICE PARAMETERS
SERVICE_NAME=kpe
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ services:
- .env
volumes:
- $ASSETS_PATH_ON_HOST:$ASSETS_PATH_IN_CONTAINER:ro
#runtime: nvidia
runtime: nvidia
3 changes: 2 additions & 1 deletion docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ else
fi
/usr/src/app/wait-for-it.sh $(echo $SERVICES_BROKER | cut -d'/' -f 3) --timeout=20 --strict -- echo " $SERVICES_BROKER (Service Broker) is up"
echo "RUNNING CELERY WORKER"
celery --app=celery_app.celeryapp worker -Ofair -n nlp_${SERVICE_NAME}_worker@%h --queues=${SERVICE_NAME} -c ${CONCURRENCY}
POOL=$([ $USE_GPU == "True" ] && echo "gevent" || echo "prefork")
celery --app=celery_app.celeryapp worker -Ofair -n nlp_${SERVICE_NAME}_worker@%h --queues=${SERVICE_NAME} -c ${CONCURRENCY} --pool=$POOL
else
echo "ERROR: Wrong serving command: $1"
exit -1
Expand Down
14 changes: 11 additions & 3 deletions http_server/ingress.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import os
import json
import logging
from time import time
Expand All @@ -8,7 +9,7 @@
import components

from flask import Flask, request, abort, Response, json
from serving import GunicornServing
from serving import GeventServing, GunicornServing
from confparser import createParser
from swagger import setupSwaggerUI

Expand Down Expand Up @@ -86,8 +87,15 @@ def server_error(error):
logger.debug("Swagger UI set.")
except Exception as e:
logger.warning("Could not setup swagger: {}".format(str(e)))

serving = GunicornServing(app, {'bind': '{}:{}'.format("0.0.0.0", args.service_port),

if os.environ.get("USE_GPU", "True") == "True":
serving_type = GeventServing
logger.debug("Serving with gevent")
else:
serving_type = GunicornServing
logger.debug("Serving with gunicorn")

serving = serving_type(app, {'bind': '{}:{}'.format("0.0.0.0", args.service_port),
'workers': args.workers,})
logger.info(args)
try:
Expand Down
24 changes: 23 additions & 1 deletion http_server/serving.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import gunicorn.app.base
import gevent.pywsgi
import gevent.monkey
gevent.monkey.patch_all()

class GunicornServing(gunicorn.app.base.BaseApplication):

Expand All @@ -14,4 +17,23 @@ def load_config(self):
self.cfg.set(key.lower(), value)

def load(self):
return self.application
return self.application

class GeventServing():

def __init__(self, app, options=None):
self.options = options or {}
self.application = app

def run(self):
bind = self.options.get('bind', "0.0.0.0:8080")
workers = self.options.get('workers', 1)
listener = bind.split(':')
try:
assert len(listener) == 2
listener = (listener[0], int(listener[1]))
except:
print(f"Invalid bind address {bind}")

server = gevent.pywsgi.WSGIServer(listener, self.application, spawn = workers)
server.serve_forever()

0 comments on commit 5a1b5d3

Please sign in to comment.