1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
.. _aio-pika: https://github.com/mosquito/aio-pika
Patterns and helpers
++++++++++++++++++++
.. note:: Available since `aio-pika>=1.7.0`
`aio-pika`_ includes some useful patterns for creating distributed systems.
.. _patterns-worker:
Master/Worker
~~~~~~~~~~~~~
Helper which implements Master/Worker pattern.
This applicable for balancing tasks between multiple workers.
The master creates tasks:
.. literalinclude:: examples/master.py
:language: python
Worker code:
.. literalinclude:: examples/worker.py
:language: python
The one or multiple workers executes tasks.
.. _patterns-rpc:
RPC
~~~
Helper which implements Remote Procedure Call pattern.
This applicable for balancing tasks between multiple workers.
The caller creates tasks and awaiting results:
.. literalinclude:: examples/rpc-caller.py
:language: python
One or multiple callees executing tasks:
.. literalinclude:: examples/rpc-callee.py
:language: python
Extending
~~~~~~~~~
Both patterns serialization behaviour might be changed by inheritance and
redefinition of methods :func:`aio_pika.patterns.base.serialize`
and :func:`aio_pika.patterns.base.deserialize`.
Following examples demonstrates it:
.. literalinclude:: examples/extend-patterns.py
:language: python
|