Skip to content

Instantly share code, notes, and snippets.

@weird-ego
Created August 16, 2020 16:06
Show Gist options
  • Save weird-ego/5909528db5a8fdb7749090642510bb39 to your computer and use it in GitHub Desktop.
Save weird-ego/5909528db5a8fdb7749090642510bb39 to your computer and use it in GitHub Desktop.
QMAAS (quick math as a service) strikes again
version: '2.0'
services:
fib:
build: .
ports:
- "8000:8000"
depends_on:
- redis
redis:
image: "redis:alpine"
ports:
- "6379:6379"
FROM snakepacker/python:all as builder
RUN python3.8 -m venv /usr/share/python3/app
RUN /usr/share/python3/app/bin/pip install -U pip redis falcon gunicorn requests
RUN find-libdeps /usr/share/python3/app > /usr/share/python3/app/pkgdeps.txt
FROM snakepacker/python:3.8
COPY --from=builder /usr/share/python3/app /usr/share/python3/app
RUN cat /usr/share/python3/app/pkgdeps.txt | xargs apt-install
RUN ln -snf /usr/share/python3/app/bin/gunicorn /usr/bin/
RUN mkdir /app && echo 'import redis; import falcon; import requests as rq \n\
redis = redis.Redis(host="redis", port=6379, db=0)\n\
redis.flushall()\n\
redis.set(1, 0)\n\
redis.set(2, 1)\n\
api = falcon.API()\n\
\n\
class CachedFib:\n\
def fib(self, n):\n\
if fib_of_n := redis.get(n):\n\
return int(fib_of_n)\n\
else:\n\
fib_of_n = int(self.fib(n - 2) + self.fib(n - 1))\n\
redis.set(n, fib_of_n)\n\
return fib_of_n\n\
\n\
def on_get(self, req, res, n):\n\
try:\n\
n = int(n)\n\
assert n > 0, f"invalid argument {n}"\n\
res.body = str(self.fib(n))\n\
except Exception as e:\n\
res.body = str(e)\n\
\n\
api.add_route("/fib/{n}", CachedFib())' > /app/fib.py
CMD sleep 5 && cd /app && gunicorn --bind=0:8000 fib:api
docker build -t fib .
docker-compose -f docker-compose.yml up -d
sleep 10
for i in {asd,-1,1,2,3,4,5,6,7,8,9};
do echo "fib($i) = $(curl -s http://127.0.0.1:8000/fib/$i)";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment