Skip to content

A small exercise published as requested late 2019

Notifications You must be signed in to change notification settings

m-hu/small-exercise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Language grade: Python

README

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.

Prerequisites

  • 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.

Installation

  • On a linux work station directly
    • Install python dependencies
      pip install -r requirements.txt
    • Install using setup.py
      python setup.py install
  • Using container tool
    • Build docker image
      docker build -t sum_cal .

How to use?

  • 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
      import requests
      import json
      r = requests.post("http://localhost:8000/total/", data=json.dumps(list(range(10000001))))
      print(r.json())
      expected output
      {'total': 50000005000000}
    • submit some calculation requests in parallel
      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")
      expected output
      ok at this scale

How to develop?

  • 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

About

A small exercise published as requested late 2019

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published