Giter Site home page Giter Site logo

rest_queue's Introduction

rest_queue

rest_queue is a simple demonstration of creating a REST service to host one or more FIFO queues, shareable across multiple producers and consumers.

Background

rest_queue originated out of some discussion on the StackOverflow Python chat, regarding sharing of a queue across multiple Python processes. The initial question was how to connect to a queue that had already been created in a running Python program. Several alternatives were posed, but most were very low-level, or did not support access by new external Python processes.

My suggestion was to create a small REST service that would manage a single queue, which any Python script could then connect to and push or pop items from the queue. All that would be needed would be the base URL of the REST service. So after the discussion died down, I made an attempt at creating my own such REST service.

I chose FastAPI as the framework with which to create the REST service, as I wanted to make a small project with it to learn more about it. The actual implementation was very straightforward, once I settled on the API endpoints.

I also extended the original concept, where the server managed just a single queue, to support multiple queues, to be a better illustration of REST interactions with a server managing multiple resources.

REST API

Function REST method URL Return status
Create Queue POST /queues/{queue_name} 201 - Queue created
409 - Queue exists
Queue Push POST /queues/{queue_name}/push 200 - Operation successful
404 - No such queue
Queue Pop POST /queues/{queue_name}/pop 200 - Operation successful
404 - No such queue
List Queue Items GET /queues/{queue_name} 200 - Operation successful
404 - No such queue
Delete Queue DELETE /queues/{queue_name}[?safe_delete=true] 200 - Operation successful
404 - No such queue
409 - Queue is not empty (safe_mode=True)
List All Queues GET /queues 200 - Operation successful

Deploying

Manual

The rest_queue server can be run locally by entering commands at the terminal console. Install Python 3.6 or later, and after cd to the project's rest_queue/src directory, install the required Python add-on modules using:

pip install -r requirements.txt

Then run the server using uvicorn:

uvicorn rest_queue:app --port 8001

You can open a browser to http://localhost:8001/docs#/ to interact with the new queue.

Docker

The docker directory includes a Dockerfile to simplify creating a rest_queue running in a Docker container. cd to this project's docker directory, and run the following commands to build, run, stop, start, and delete the rest_queue docker image and container.

# build image
docker build -t rest_queue:latest .

# start container mapping port 18000 to container port 8000
docker run -d -p 18000:8000 --name rest_queue rest_queue

# stop running container
docker stop rest_queue

# start stopped container
docker start rest_queue

# view running containers
docker ps

# delete container
docker rm rest_queue

# view docker images
docker images

# delete image
docker rmi rest_queue

Open a browser to http://localhost:18000/docs#/ to interact with the new queue.

rest_queue's People

Contributors

ptmcg avatar

Watchers

 avatar

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.