This exercise demonstrates a small http web server providing a RESTful API endpoint to calculate the sum of a list of integers in stream mode
- endpoints
- /total/
- method: post
- payload: [1, 2, 3, 4]
- a list of integers in json format. Other format or number type may cause an error message.
- response: { "total": 10 }
- a dictionary which contains a key named "total" whose value is the sum, in json format.
- /total/
- In order to benefit from Tornado's full capability, please run it on Linux. Otherwise, please just use docker instead.
- The current implementation is tested against Python 3.6, which is normally defined by the embedded Dockerfile.
- On a linux work station directly
- Install python dependencies
pip install -r requirements.txt
- Install using setup.py
python setup.py install
- Install python dependencies
- Using container tool
- Build docker image
docker build -t sum_cal .
- Build docker image
-
Launch the web server after an installation with Python
calsum --http-address=0.0.0.0 --app-debug=false --enable-auto-fork
-
Launch it using docker-compose
docker-compose -f docker-compose.yml build docker-compose -f docker-compose.yml up
-
Test it with a small python snippet
- submit a big calculation
expected output
import requests import json r = requests.post("http://localhost:8000/total/", data=json.dumps(list(range(10000001)))) print(r.json())
{'total': 50000005000000}
- submit some calculation requests in parallel
expected output
import requests import json from multiprocessing import Pool def test_request(x): r = requests.post("http://localhost:8000/total/", data=json.dumps(list(range(100001+x)))) return json.loads(r.content.decode()).get("total") def gen_result(x): return sum(range(100001 + x)) with Pool(10) as p: expected = [gen_result(x) for x in range(100)] for e, r in zip(expected, p.map(test_request, [_ for _ in range(100)])): if e != r: raise Exception("{}!={}".format(e, r)) print("ok at this scale")
ok at this scale
- submit a big calculation
-
Install dependencies
pip install -r requirements.txt
-
Install in development mode and run the server
python setup.py develop calsum --http-address=0.0.0.0 --app-debug=true
-
Run tests
python setup.py test