Git Product home page Git Product logo

flask-redis-docker's Introduction

flask-redis-docker

An example about how to deploy a Flask application inside a Docker able to perform asynchronous, parallel requests, with the Flask application handled by NGINX/uWSGI.

How it works

Though Python allows asynchronous executions through several techniques, forking asynchronous processes from the uWSGI plugin is not allowed (at least by default), and not recommended (see https://docs.docker.com/config/containers/multi-service_container/). A good solution is to define a worker process within the main processes of the container (i.e. handled through supervisor, used as main process defined in the container's CMD), that is able to perform operations in background.

The repo has two sections, one dedicated to Python 2.7 (based on RedisQueue) and one to Python 3.6 (based on Celery). Both examples use Redis as broker messages.

How to run it

Once you have docker-compose installed, just create the images and the containers through the command:

$ docker-compose up -d

This will create the two containers, the one with the Flask application listening port 5000, and the one with the Redis service (listening port 6379).

$ docker-compose ps
 Name               Command               State               Ports            
-------------------------------------------------------------------------------
redis    docker-entrypoint.sh redis ...   Up      6379/tcp                     
webapp   /entrypoint.sh /start.sh         Up      443/tcp, 0.0.0.0:5000->80/tcp

Once both services are up and running in your localhost, you can submit a long running request asynchronously as follows:

$ curl -X POST -F "duration=20" http://127.0.0.1:5000/long_task

and for a parallelised task:

$ curl -X POST -F "duration=200" http://127.0.0.1:5000/parallel_long_task

Then the outputs can be retrieved as follows:

$ curl -X GET http://127.0.0.1:5000/task/<task_id>

or, in the case of a parallel task:

$ curl -X GET http://127.0.0.1:5000/parallel_task/<task_id>

flask-redis-docker's People

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.