Skip to content

Commit 5a1b5d3

Browse files
committed
gpu
1 parent 61a8928 commit 5a1b5d3

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

.envdefault

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ LM_MAP = "{
1010
# SERVING PARAMETERS
1111
SERVICE_MODE=http
1212
CONCURRENCY=1
13+
USE_GPU=True
1314

1415
# MICRO-SERVICE PARAMETERS
1516
SERVICE_NAME=kpe

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ services:
99
- .env
1010
volumes:
1111
- $ASSETS_PATH_ON_HOST:$ASSETS_PATH_IN_CONTAINER:ro
12-
#runtime: nvidia
12+
runtime: nvidia

docker-entrypoint.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ else
2222
fi
2323
/usr/src/app/wait-for-it.sh $(echo $SERVICES_BROKER | cut -d'/' -f 3) --timeout=20 --strict -- echo " $SERVICES_BROKER (Service Broker) is up"
2424
echo "RUNNING CELERY WORKER"
25-
celery --app=celery_app.celeryapp worker -Ofair -n nlp_${SERVICE_NAME}_worker@%h --queues=${SERVICE_NAME} -c ${CONCURRENCY}
25+
POOL=$([ $USE_GPU == "True" ] && echo "gevent" || echo "prefork")
26+
celery --app=celery_app.celeryapp worker -Ofair -n nlp_${SERVICE_NAME}_worker@%h --queues=${SERVICE_NAME} -c ${CONCURRENCY} --pool=$POOL
2627
else
2728
echo "ERROR: Wrong serving command: $1"
2829
exit -1

http_server/ingress.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3+
import os
34
import json
45
import logging
56
from time import time
@@ -8,7 +9,7 @@
89
import components
910

1011
from flask import Flask, request, abort, Response, json
11-
from serving import GunicornServing
12+
from serving import GeventServing, GunicornServing
1213
from confparser import createParser
1314
from swagger import setupSwaggerUI
1415

@@ -86,8 +87,15 @@ def server_error(error):
8687
logger.debug("Swagger UI set.")
8788
except Exception as e:
8889
logger.warning("Could not setup swagger: {}".format(str(e)))
89-
90-
serving = GunicornServing(app, {'bind': '{}:{}'.format("0.0.0.0", args.service_port),
90+
91+
if os.environ.get("USE_GPU", "True") == "True":
92+
serving_type = GeventServing
93+
logger.debug("Serving with gevent")
94+
else:
95+
serving_type = GunicornServing
96+
logger.debug("Serving with gunicorn")
97+
98+
serving = serving_type(app, {'bind': '{}:{}'.format("0.0.0.0", args.service_port),
9199
'workers': args.workers,})
92100
logger.info(args)
93101
try:

http_server/serving.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import gunicorn.app.base
2+
import gevent.pywsgi
3+
import gevent.monkey
4+
gevent.monkey.patch_all()
25

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

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

1619
def load(self):
17-
return self.application
20+
return self.application
21+
22+
class GeventServing():
23+
24+
def __init__(self, app, options=None):
25+
self.options = options or {}
26+
self.application = app
27+
28+
def run(self):
29+
bind = self.options.get('bind', "0.0.0.0:8080")
30+
workers = self.options.get('workers', 1)
31+
listener = bind.split(':')
32+
try:
33+
assert len(listener) == 2
34+
listener = (listener[0], int(listener[1]))
35+
except:
36+
print(f"Invalid bind address {bind}")
37+
38+
server = gevent.pywsgi.WSGIServer(listener, self.application, spawn = workers)
39+
server.serve_forever()

0 commit comments

Comments
 (0)