PK!rflask_dramatiq.pyfrom importlib import import_module from threading import local import click from dramatiq import ( Middleware, actor as register_actor, set_broker, ) from dramatiq.cli import ( CPUS, main as dramatiq_worker, make_argument_parser as dramatiq_argument_parser, ) from flask.cli import with_appcontext def import_object(path): # Implement setuptools entrypoint-like loading of object. modname, objname = path.split(':') mod = import_module(modname) return getattr(mod, objname) class AppContextMiddleware(Middleware): # Setup Flask app for actor. Borrowed from # https://github.com/Bogdanp/flask_dramatiq_example. 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: context = self.state.context context.pop(exception) del self.state.context except AttributeError: pass after_skip_message = after_process_message class Dramatiq: # The Flask extension. def __init__(self, app=None): self.actors = [] self.app = app if app: self.init_app(app) def init_app(self, app): # Reuse same defaults as dramatiq. cf. # https://github.com/Bogdanp/dramatiq/blob/master/dramatiq/broker.py#L34-L44 app.config.setdefault( 'DRAMATIQ_BROKER', 'dramatiq.brokers.rabbitmq:RabbitmqBroker') app.config.setdefault('DRAMATIQ_BROKER_URL', None) cls = import_object(app.config['DRAMATIQ_BROKER']) broker = cls(url=app.config['DRAMATIQ_BROKER_URL']) broker.add_middleware(AppContextMiddleware(app)) set_broker(broker) for actor in self.actors: actor.register() def actor(self, fn=None, **kw): # Substitude dramatiq.actor decorator to return a lazy wrapper. This # allow to register actors in extension before the broker is # effectively configured by init_app. def decorator(fn): lazy_actor = LazyActor(self, fn, kw) self.actors.append(lazy_actor) if self.app: lazy_actor.register() return lazy_actor if fn: return decorator(fn) return decorator class LazyActor(object): # Intermediate object that register actor on broker an call. def __init__(self, extension, fn, kw): self.extension = extension self.fn = fn self.kw = kw self.actor = None def register(self): self.actor = register_actor(**self.kw)(self.fn) # Next is regular actor API. def send(self, *a, **kw): return self.actor.send(*a, **kw) def send_with_options(self, *a, **kw): return self.actor.send_with_options(*a, **kw) @click.command() @click.option('-p', '--processes', default=CPUS, metavar='PROCESSES', show_default=True, help="the number of worker processes to run") @click.option('-t', '--threads', default=8, metavar='THREADS', show_default=True, help="the number of worker treads per processes") @click.option('-q', '--queues', type=str, default=None, metavar='QUEUES', show_default=True, help="listen to a subset of queues, comma separated") @with_appcontext def worker(processes, threads, queues): """ Run dramatiq workers. Setup Dramatiq with broker and task modules from Flask app. \b examples: # Run dramatiq with 1 thread per process. $ flask worker --threads 1 \b # Listen only to the "foo" and "bar" queues. $ flask worker -Q foo,bar """ # Plugin for flask.commands entrypoint. # # Wraps dramatiq worker CLI in a Flask command. This is private API of # dramatiq. parser = dramatiq_argument_parser() command = [ "--processes", str(processes), "--threads", str(threads), __name__, ] if queues: command += ["--queues"] + queues.split(",") args = parser.parse_args(command) dramatiq_worker(args) PK!H##H%)//flask_dramatiq-0.2.1.dist-info/entry_points.txtNI,KMK)*/N- ǧ%&dZADPK!M&flask_dramatiq-0.2.1.dist-info/LICENSECopyright 2019 Étienne BERSAC Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.PK!HnHTU$flask_dramatiq-0.2.1.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!H+xR'flask_dramatiq-0.2.1.dist-info/METADATAUn6}WLw`l- l6hR'ۗ(i,H]u (9s;3^‹ONj~>XUmŠZxyF!&0+ 1F[^V7>T2'V7]cdDIıɵ-ccߘxQ928GY%l jvxH.%䘿=x?dd\I$\]̐;,™ wLZ]RPT%\ U6FwK-~yLq.O`(Lޟ޽S6G[iBѣ3<*]o ハ{B+^Cw>1c{LՔn_}{.jnZGPRntuh{Mp5|DJ(ޕ,u/Gp\b@ Y őE^`B^ ڔI̪Ȏm \Bam:wqwo'E ^(EUut7G~RuʤX@ ;Cwܺ$h8/ff,O_~?_Tw ɬ&AFArIt_"ˤo8m?P7x,#3,-. ,CIVӴUBljZR>{nrt* jtAȮ4>`NOsa75B&MIPavzɣsCwrԵ`FTH燝|G#onACn;8QOwY3ڔΣ> mWݽG"AhWav0kFcI!*ni36!z:aiz+=ViKBD9 ͩ. L;>&.PK!HE-%flask_dramatiq-0.2.1.dist-info/RECORD˶BPy߂qwq+:MZ(#&5g<=,/z7?kY`{o;ex~TQ)&'[ed&on8׮-0g\8tРlo&UiMtc^/* ˨&ۭ- -x[ q2v/#;0r! \PkZtM߯!{ݹzPٿCOh40& iOR[)%kIi厐Kjyr3g9jgh[HuD v*EmPK!rflask_dramatiq.pyPK!H##H%)//flask_dramatiq-0.2.1.dist-info/entry_points.txtPK!M&flask_dramatiq-0.2.1.dist-info/LICENSEPK!HnHTU$flask_dramatiq-0.2.1.dist-info/WHEELPK!H+xR'flask_dramatiq-0.2.1.dist-info/METADATAPK!HE-%Hflask_dramatiq-0.2.1.dist-info/RECORDPK