From fbf0384985c056ae46bb8e073ba820e6a9bd6b6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Thu, 2 Feb 2017 16:35:58 +0100 Subject: [PATCH] Replaced gevent with threading * Replaced gevent (testing) * Trying the slim python image (1/3 of previous size) --- Dockerfile | 2 +- Dockerfile-2.7 | 6 +++++- Dockerfile-3.4 | 6 +++++- Dockerfile-3.5 | 9 +++++++++ Dockerfile.template | 6 +++++- MANIFEST.in | 2 +- Makefile | 5 +++-- senpy/VERSION | 2 +- senpy/extensions.py | 36 +++++++++++++++++++----------------- setup.py | 7 +++---- 10 files changed, 52 insertions(+), 29 deletions(-) create mode 100644 Dockerfile-3.5 diff --git a/Dockerfile b/Dockerfile index ea99ab1..89e531a 120000 --- a/Dockerfile +++ b/Dockerfile @@ -1 +1 @@ -Dockerfile-3.4 \ No newline at end of file +Dockerfile-3.5 \ No newline at end of file diff --git a/Dockerfile-2.7 b/Dockerfile-2.7 index c4953ad..8589bcf 100644 --- a/Dockerfile-2.7 +++ b/Dockerfile-2.7 @@ -1,5 +1,9 @@ -from python:2.7-onbuild +from python:2.7-slim +WORKDIR /usr/src/app +ADD requirements.txt /usr/src/app/ +RUN pip install -r requirements.txt +ADD . /usr/src/app/ RUN pip install . ENTRYPOINT ["python", "-m", "senpy", "-f", ".", "--host", "0.0.0.0"] diff --git a/Dockerfile-3.4 b/Dockerfile-3.4 index a6e979e..15867bc 100644 --- a/Dockerfile-3.4 +++ b/Dockerfile-3.4 @@ -1,5 +1,9 @@ -from python:3.4-onbuild +from python:3.4-slim +WORKDIR /usr/src/app +ADD requirements.txt /usr/src/app/ +RUN pip install -r requirements.txt +ADD . /usr/src/app/ RUN pip install . ENTRYPOINT ["python", "-m", "senpy", "-f", ".", "--host", "0.0.0.0"] diff --git a/Dockerfile-3.5 b/Dockerfile-3.5 new file mode 100644 index 0000000..52dd691 --- /dev/null +++ b/Dockerfile-3.5 @@ -0,0 +1,9 @@ +from python:3.5-slim + +WORKDIR /usr/src/app +ADD requirements.txt /usr/src/app/ +RUN pip install -r requirements.txt +ADD . /usr/src/app/ +RUN pip install . + +ENTRYPOINT ["python", "-m", "senpy", "-f", ".", "--host", "0.0.0.0"] diff --git a/Dockerfile.template b/Dockerfile.template index a75a9cb..cc04038 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -1,5 +1,9 @@ -from python:{{PYVERSION}}-onbuild +from python:{{PYVERSION}}-slim +WORKDIR /usr/src/app +ADD requirements.txt /usr/src/app/ +RUN pip install -r requirements.txt +ADD . /usr/src/app/ RUN pip install . ENTRYPOINT ["python", "-m", "senpy", "-f", ".", "--host", "0.0.0.0"] diff --git a/MANIFEST.in b/MANIFEST.in index e6cb3ac..10d2ca8 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -7,4 +7,4 @@ graft senpy/plugins graft senpy/schemas graft senpy/templates graft senpy/static -graft img +graft img \ No newline at end of file diff --git a/Makefile b/Makefile index 8bdf433..45a06e5 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -PYVERSIONS=3.4 2.7 +PYVERSIONS=3.5 3.4 2.7 PYMAIN=$(firstword $(PYVERSIONS)) NAME=senpy REPO=gsiupm @@ -16,6 +16,7 @@ dev: pre-commit install dockerfiles: $(addprefix Dockerfile-,$(PYVERSIONS)) + @unlink Dockerfile >/dev/null ln -s Dockerfile-$(PYMAIN) Dockerfile Dockerfile-%: Dockerfile.template @@ -35,7 +36,7 @@ test: $(addprefix test-,$(PYMAIN)) testall: $(addprefix test-,$(PYVERSIONS)) -debug-%: build-debug-% +debug-%: docker run --rm -w /usr/src/app/ -v $$PWD:/usr/src/app --entrypoint=/bin/bash -ti $(NAME)-debug ; debug: debug-$(PYMAIN) diff --git a/senpy/VERSION b/senpy/VERSION index 142f9ea..c4fdcc6 100644 --- a/senpy/VERSION +++ b/senpy/VERSION @@ -1 +1 @@ -0.7.0-dev2 +0.7.0-dev3 diff --git a/senpy/extensions.py b/senpy/extensions.py index fe36249..1583d96 100644 --- a/senpy/extensions.py +++ b/senpy/extensions.py @@ -2,9 +2,6 @@ """ from future import standard_library standard_library.install_aliases() -import gevent -from gevent import monkey -monkey.patch_all() from .plugins import SentimentPlugin from .models import Error @@ -12,7 +9,8 @@ from .blueprints import api_blueprint, demo_blueprint from .api import API_PARAMS, NIF_PARAMS, parse_params from git import Repo, InvalidGitRepositoryError -from functools import partial + +from threading import Thread import os import fnmatch @@ -154,20 +152,23 @@ class Senpy(object): logger.info("Activating plugin: {}".format(plugin.name)) def act(): + success = False try: plugin.activate() - logger.info("Plugin activated: {}".format(plugin.name)) + msg = "Plugin activated: {}".format(plugin.name) + logger.info(msg) + success = True + self._set_active_plugin(plugin_name, success) except Exception as ex: - logger.error("Error activating plugin {}: {}".format( - plugin.name, ex)) - logger.error("Trace: {}".format(traceback.format_exc())) - - th = gevent.spawn(act) - th.link_value(partial(self._set_active_plugin, plugin_name, True)) + msg = "Error activating plugin {} - {} : \n\t{}".format( + plugin.name, ex, ex.format_exc()) + logger.error(msg) + raise Error(msg) if sync: - th.join() + act() else: - return th + th = Thread(target=act) + th.start() def deactivate_plugin(self, plugin_name, sync=False): try: @@ -176,6 +177,8 @@ class Senpy(object): raise Error( message="Plugin not found: {}".format(plugin_name), status=404) + self._set_active_plugin(plugin_name, False) + def deact(): try: plugin.deactivate() @@ -185,12 +188,11 @@ class Senpy(object): plugin.name, ex)) logger.error("Trace: {}".format(traceback.format_exc())) - th = gevent.spawn(deact) - th.link_value(partial(self._set_active_plugin, plugin_name, False)) if sync: - th.join() + deact() else: - return th + th = Thread(target=deact) + th.start() def reload_plugin(self, name): logger.debug("Reloading {}".format(name)) diff --git a/setup.py b/setup.py index a47fa6d..f33fe4f 100644 --- a/setup.py +++ b/setup.py @@ -23,10 +23,9 @@ setup( name='senpy', packages=['senpy'], # this must be the same as the name above version=__version__, - description=''' - A sentiment analysis server implementation. Designed to be \ -extendable, so new algorithms and sources can be used. - ''', + description=('A sentiment analysis server implementation. ' + 'Designed to be extensible, so new algorithms ' + 'and sources can be used.'), author='J. Fernando Sanchez', author_email='balkian@gmail.com', url='https://github.com/gsi-upm/senpy', # use the URL to the github repo