def init_actor(self):
     """
     Инициализирует dramatiq actor
     """
     self.actor = dramatiq.actor(*self.dec_args, **self.dec_kwargs)(
         self.orig_func
     )
Example #2
0
    def __new__(metacls, name, bases, attrs):
        clazz = super().__new__(metacls, name, bases, attrs)
        meta = getattr(clazz, "Meta", object())
        if not getattr(meta, "abstract", False):
            options = {}

            # Use meta's inherited attributes
            ignored = ('abstract',)

            for base in reversed(meta.__mro__):
                options.update({
                    name: getattr(base, name) for name in vars(base) if not name.startswith('_') and name not in ignored
                })

            # Include the module in the actor name
            default_name = '.'.join(clazz.__module__.split('.')[:2]) + '.' + clazz.__name__
            actor_name = getattr(meta, 'actor_name', default_name)
            options.pop('actor_name', None)

            def fn(*args, **kwargs):
                """Create a new instance of the class for each call. This lets us attach a TaskContext to each
                individual instance."""
                new = clazz()
                return new(*args, **kwargs)

            clazz_instance = clazz()
            actor_instance = dramatiq.actor(fn, actor_name=actor_name, **options)
            setattr(clazz, "__getattr__", generic_actor.__getattr__)
            setattr(clazz_instance, "__actor__", actor_instance)
            return clazz_instance

        setattr(meta, "abstract", False)
        return clazz
Example #3
0
    def actor(self, fn=None, **kw):
        """Declare an actor for this broker instance.

        Calls :func:`dramatiq.actor` internally.

        Example::

          from flask_melodramatiq import Broker

          broker = Broker()

          @broker.actor
          def task():
              print('Snakes appreciate good theatrical preformace.')
        """

        for kwarg in ['broker', 'actor_class']:
            if kwarg in kw:
                raise TypeError(
                    '{class_name}.actor() got an unexpected keyword argument "{kwarg}".'
                    .format(
                        class_name=type(self).__name__,
                        kwarg=kwarg,
                    ))
        decorator = dramatiq.actor(actor_class=LazyActor, broker=self, **kw)
        if fn is None:
            return decorator
        return decorator(fn)
Example #4
0
    def decorator(fn):
        @functools.wraps(fn)
        def wrapper(*args, **kwargs):
            result = fn(*args, **kwargs)
            db._session.remove()
            return result

        return dramatiq.actor(wrapper)
Example #5
0
    def __init__(self, broker):
        self.blocking_sem = threading.Semaphore(0)
        self.actor_calls = []
        self.actor_call_times = []

        # note we wrap these as actors here rather than using decorators so that
        # we can wait until the test creates a broker instance
        self.basic = dramatiq.actor(self.basic, broker=broker)
        self.basic_other_queue = dramatiq.actor(
            self.basic_other_queue, queue_name="other-queue", broker=broker
        )
        self.always_fails = dramatiq.actor(
            self.always_fails, broker=broker, max_backoff=0.001, max_retries=1
        )
        self.succeeds_on_retry = dramatiq.actor(
            self.succeeds_on_retry, broker=broker, max_backoff=0.001
        )
        self.blocking_fn = dramatiq.actor(self.blocking_fn, broker=broker)
Example #6
0
    def set_broker(self, broker):
        import dramatiq

        self.broker = broker
        dramatiq.set_broker(broker)

        self.func_to_actor = {}
        for func, actor_kwargs in self.func_to_actor_kwargs.items():
            self.func_to_actor[func] = dramatiq.actor(**actor_kwargs)(func)
Example #7
0
def initialize_context(app: Flask, event_handler: Callable):

    dramatiq.set_broker(broker)
    broker.add_middleware(AppContextMiddleware(app))

    dramatiq_actor = dramatiq.actor(fn=lambda *args, **kwargs: event_handler,
                                    broker=broker,
                                    max_retries=3)

    builtins.DramatiqEvent = dramatiq_actor

    return dramatiq_actor
Example #8
0
 def wrap(func):
     if 'test' not in sys.argv:
         return dramatiq.actor(func, **kwargs)
     func.send = func
     return func
Example #9
0
    p = random.randint(1, 100)
    if p == 1:
        duration = 10
    elif p <= 30:
        duration = 5
    elif p <= 50:
        duration = 3
    else:
        duration = 1

    time.sleep(duration)
    with memcache_pool.reserve() as client:
        client.incr(counter_key)


dramatiq_fib_bench = dramatiq.actor(fib_bench)
dramatiq_latency_bench = dramatiq.actor(latency_bench)

celery_fib_bench = celery_app.task(name="fib-bench", acks_late=True)(fib_bench)
celery_latency_bench = celery_app.task(name="latency-bench",
                                       acks_late=True)(latency_bench)


def benchmark_arg(value):
    benchmarks = ("fib", "latency")
    if value not in benchmarks:
        raise argparse.ArgumentTypeError(
            f"benchmark must be one of {benchmarks!r}")
    return value

Example #10
0
 def decorator(fn):
     return dramatiq.actor(_inject(fn), **kwargs)
Example #11
0
                       spider=spider)


@dramatiq.actor(max_age=st.MAX_AGE,
                max_retries=st.MAX_RETRIES,
                min_backoff=st.MIN_BACKOFF,
                time_limit=st.TIME_LIMIT)
def on_failure():
    pass


@dramatiq.actor(max_age=st.MAX_AGE,
                max_retries=st.MAX_RETRIES,
                min_backoff=st.MIN_BACKOFF,
                time_limit=st.TIME_LIMIT)
def on_success():
    pass


if STORE_RESULTS:
    crawler = dramatiq.actor(store_results=True,
                             max_age=st.MAX_AGE,
                             max_retries=st.MAX_RETRIES,
                             min_backoff=st.MIN_BACKOFF,
                             time_limit=st.TIME_LIMIT)(_crawler)
else:
    crawler = dramatiq.actor(max_age=st.MAX_AGE,
                             max_retries=st.MAX_RETRIES,
                             min_backoff=st.MIN_BACKOFF,
                             time_limit=st.TIME_LIMIT)(_crawler)