How to use the dramatiq.set_broker function in dramatiq

To help you get started, we’ve selected a few dramatiq examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Bogdanp / dramatiq / examples / persistent / example.py View on Github external
import argparse
import json
import os
import sys
import time

import dramatiq
from dramatiq.middleware import Shutdown

if os.getenv("REDIS") == "1":
    from dramatiq.brokers.redis import RedisBroker
    broker = RedisBroker()
    dramatiq.set_broker(broker)


def path_to(*xs):
    return os.path.abspath(os.path.join(os.path.dirname(__file__), *(str(x) for x in xs)))


def load_state(n):
    try:
        with open(path_to("states", n), "r") as f:
            data = json.load(f)
    except Exception:
        fib.logger.info("Could not read state file, using defaults.")
        return 1, 1, 0

    i, x2, x1 = data["i"], data["x2"], data["x1"]
    fib.logger.info("Resuming fib(%d) from iteration %d.", n, i)
github Bogdanp / dramatiq / benchmarks / bench.py View on Github external
import celery
import pylibmc

import dramatiq
from dramatiq.brokers.rabbitmq import RabbitmqBroker
from dramatiq.brokers.redis import RedisBroker

logger = logging.getLogger("example")
counter_key = "latench-bench-counter"
memcache_client = pylibmc.Client(["localhost"], binary=True)
memcache_pool = pylibmc.ClientPool(memcache_client, 8)
random.seed(1337)

if os.getenv("REDIS") == "1":
    broker = RedisBroker()
    dramatiq.set_broker(broker)
    celery_app = celery.Celery(broker="redis:///")

else:
    broker = RabbitmqBroker(host="127.0.0.1")
    dramatiq.set_broker(broker)
    celery_app = celery.Celery(broker="amqp:///")


def fib_bench(n):
    p, q = 0, 1
    while n > 0:
        p, q = q, p + q
        n -= 1

    with memcache_pool.reserve() as client:
        client.incr(counter_key)
github Bogdanp / dramatiq / examples / crawler / example.py View on Github external
import pylibmc
import requests

import dramatiq

logger = logging.getLogger("example")
memcache_client = pylibmc.Client(["localhost"], binary=True)
memcache_pool = pylibmc.ThreadMappedPool(memcache_client)
anchor_re = re.compile(rb'<a href="([^">')
state = local()

if os.getenv("REDIS") == "1":
    from dramatiq.brokers.redis import RedisBroker
    broker = RedisBroker()
    dramatiq.set_broker(broker)


def get_session():
    session = getattr(state, "session", None)
    if session is None:
        session = state.session = requests.Session()
    return session


@dramatiq.actor(max_retries=3, time_limit=10000)
def crawl(url):
    url_hash = hashlib.md5(url.encode("utf-8")).hexdigest()
    with memcache_pool.reserve() as client:
        added = client.add(url_hash, b"", time=3600)
        if not added:
            logger.warning("URL %r has already been visited. Skipping...", url)</a>
github Bogdanp / flask_dramatiq_example / app.py View on Github external
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import func
from threading import local


app = Flask(__name__)

database_url = os.getenv("DATABASE_URL", os.getenv("SQLITE_URL"))
database = create_engine(database_url)
Model = declarative_base()
Session = sessionmaker(bind=database)

broker_url = os.getenv("CLOUDAMQP_URL", os.getenv("BROKER_URL"))
broker = URLRabbitmqBroker(broker_url)
dramatiq.set_broker(broker)


class AppContextMiddleware(dramatiq.Middleware):
    state = local()

    def __init__(self, app):
        self.app = app

    def before_process_message(self, broker, message):
        context = self.app.app_context()
        context.push()

        self.state.context = context

    def after_process_message(self, broker, message, *, result=None, exception=None):
        try:
github Bogdanp / dramatiq / benchmarks / bench.py View on Github external
from dramatiq.brokers.rabbitmq import RabbitmqBroker

logger = logging.getLogger("example")
counter_key = "latench-bench-counter"
memcache_client = pylibmc.Client(["localhost"], binary=True)
memcache_pool = pylibmc.ClientPool(memcache_client, 8)
random.seed(1337)

if os.getenv("REDIS") == "1":
    broker = RedisBroker()
    dramatiq.set_broker(broker)
    celery_app = celery.Celery(broker="redis:///")

else:
    broker = RabbitmqBroker()
    dramatiq.set_broker(broker)
    celery_app = celery.Celery(broker="amqp:///")


def fib_bench(n):
    p, q = 0, 1
    while n > 0:
        p, q = q, p + q
        n -= 1

    with memcache_pool.reserve() as client:
        client.incr(counter_key)

    return p


def latency_bench():
github Bogdanp / dramatiq / examples / results / example.py View on Github external
import argparse
import random
import sys
import time

import dramatiq
from dramatiq.brokers.rabbitmq import RabbitmqBroker
from dramatiq.encoder import PickleEncoder
from dramatiq.results import Results
from dramatiq.results.backends import RedisBackend

result_backend = RedisBackend(encoder=PickleEncoder())
broker = RabbitmqBroker()
broker.add_middleware(Results(backend=result_backend))
dramatiq.set_broker(broker)


@dramatiq.actor(store_results=True)
def sleep_then_add(t, x, y):
    time.sleep(t)
    return x + y


def main(args):
    parser = argparse.ArgumentParser()
    parser.add_argument("count", type=int, help="the number of messages to enqueue")
    args = parser.parse_args()

    messages = []
    for _ in range(args.count):
        messages.append(sleep_then_add.send(
github Bogdanp / dramatiq / benchmarks / bench.py View on Github external
from dramatiq.brokers.redis import RedisBroker

logger = logging.getLogger("example")
counter_key = "latench-bench-counter"
memcache_client = pylibmc.Client(["localhost"], binary=True)
memcache_pool = pylibmc.ClientPool(memcache_client, 8)
random.seed(1337)

if os.getenv("REDIS") == "1":
    broker = RedisBroker()
    dramatiq.set_broker(broker)
    celery_app = celery.Celery(broker="redis:///")

else:
    broker = RabbitmqBroker(host="127.0.0.1")
    dramatiq.set_broker(broker)
    celery_app = celery.Celery(broker="amqp:///")


def fib_bench(n):
    p, q = 0, 1
    while n > 0:
        p, q = q, p + q
        n -= 1

    with memcache_pool.reserve() as client:
        client.incr(counter_key)

    return p


def latency_bench():
github MrNaif2018 / bitcart / api / settings.py View on Github external
MIDDLEWARE = [
    m() for m in (AgeLimit, ShutdownNotifications, Callbacks, Pipelines, Retries)
]

if TEST:
    broker = StubBroker(middleware=MIDDLEWARE)
    broker.emit_after("process_boot")
else:
    broker = RedisBroker(
        connection_pool=redis.ConnectionPool.from_url(REDIS_HOST), middleware=MIDDLEWARE
    )

broker.add_middleware(InitDB())
dramatiq.set_broker(broker)
github Bogdanp / django_dramatiq / django_dramatiq / apps.py View on Github external
)
            rate_limiter_backend_class = import_string(rate_limiter_backend_path)
            rate_limiter_backend_options = rate_limiter_backend_settings.get("BACKEND_OPTIONS", {})
            RATE_LIMITER_BACKEND = rate_limiter_backend_class(**rate_limiter_backend_options)

        broker_settings = cls.broker_settings()
        broker_path = broker_settings["BROKER"]
        broker_class = import_string(broker_path)
        broker_options = broker_settings.get("OPTIONS", {})
        middleware = [load_middleware(path) for path in broker_settings.get("MIDDLEWARE", [])]

        if result_backend is not None:
            middleware.append(results_middleware)

        broker = broker_class(middleware=middleware, **broker_options)
        dramatiq.set_broker(broker)