Skip to main content

Message Queue Tools for flask project

Project description

DASIMA

Description

Dasima is a library that helps you send and receive messages in Flask project. It is a simple wrapper around Kombu and support with the publisher/subscriber pattern of your Flask project.

Getting Started

Built With

  • Python >= 3.6

Prerequisites

  • Message Queue

    ex) Redis, RabbitMQ, ActiveMQ, ZeroMQ, Kafka...

Installation

Installing
$ pip install dasima
setting parameters
DASIMA_CONNECTION_HOST = "localhost" # your Message Queue host ex) redis://0.0.0.0, amqp://id:password@0.0.0.0:port
DASIMA_ACCEPT_TYPE = "json" # sending data type ex) json, pickle ...
DASIMA_EXCHANGE_SETTING = [("test_exchange", "one"),] 
# DASIMA_EXCHANGE_SETTING is list of tuples [(exchange name, type)]
# There are two types: 'all' and 'one'.

Usage

Subscriber Simple example

from dasima import Dasima
from flask import Flask


app = Flask(__name__)
app.config.update({
    "DASIMA_CONNECTION_HOST": "pyamqp://localhost:5672",
    "DASIMA_ACCEPT_TYPE": "json",
    "DASIMA_EXCHANGE_SETTING": [("test_exchange", "one"), ]
})

dasimamq = Dasima()
dasimamq.init_app(app) # Alternatively, auto init_app can be used after putting the flask app into Dasima like Dasima(app).

"""same as
@dasimamq.after_subscribe_task
def after_work_func(data, message, result):
    print("working after on task")
"""

def after_task_func(data, message, result):
    print("working after on task...")

dasimamq.resgiter_after_subscribe_task(after_task_func)

# Be able to subscribe target functions using the function 'subscribe' 
# The queue named by subscribed function name will be made, and binding it with routing key
# dasimamq.{exchange}.subscribe(routing_key) - "Route key to bind"
@dasimamq.test_exchange.subscribe(routing_key="test_routing_key")
# @dasimamq.test_exchange.subscribe - if routing key not defined, routing key is defined as function name
def test_function(x, y):
    print(x + y)
    return x + y


if __name__ == "__main__":
 # Call the function 'run_subscribers' to create queues in which consumers process the messages.
    dasimamq.run_subscribers()
    app.run(port=5050)

Publisher Simple example

from flask import Flask
from dasima import Dasima


app = Flask(__name__)
app.config.update({
    "DASIMA_CONNECTION_HOST": "pyamqp://localhost:5672",
    "DASIMA_ACCEPT_TYPE": "json",
    "DASIMA_EXCHANGE_SETTING": [("test_exchange", "one"), ]
})

dasimamq = Dasima()
dasimamq.init_app(app) # Alternatively, auto init_app is possible by putting the flask app directly into Dasima(app).


@app.route("/send")
def send_message():
    dasimamq.test_exchange.send_message(
        data={"x": 1, "y": 2},
        routing_key="test_routing_key"
    )
    return {"data": "send message successful"}


@app.route("/receive")
def send_message():
    res = dasimamq.test_exchange.send_message_and_recevie_result(
        data={"x": 1, "y": 2},
        routing_key="test_routing_key"
    )
    return {"result": res}


if __name__ == "__main__":
    app.run(port=5000)

Links

Contact

JISU JEONG - js.jeong@linewalks.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dasima-1.0.3-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file dasima-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: dasima-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for dasima-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 24b5c8fd0664e7952b18de95b5920662abe7e07769e19d15fd2bc6523a6d5a07
MD5 098d28af3509c1edcd529c34dfa49314
BLAKE2b-256 4371306dd0d1c6f02c044b750160754e00a864acde558c398637ea38f04ef64e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page