Coder Social home page Coder Social logo

small-exercise's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.