| 1 |
.. _implementation_details: |
| 2 |
|
| 3 |
====================== |
| 4 |
Implementation Details |
| 5 |
====================== |
| 6 |
|
| 7 |
Up to version 0.3 kazoo used the Python bindings to the Zookeeper C library. |
| 8 |
Unfortunately those bindings are fairly buggy and required a fair share of |
| 9 |
weird workarounds to interface with the native OS thread used in those |
| 10 |
bindings. |
| 11 |
|
| 12 |
Starting with version 0.4 kazoo implements the entire Zookeeper wire protocol |
| 13 |
itself in pure Python. Doing so removed the need for the workarounds and made |
| 14 |
it much easier to implement the features missing in the C bindings. |
| 15 |
|
| 16 |
Handlers |
| 17 |
======== |
| 18 |
|
| 19 |
Both the Kazoo handlers run 3 separate queues to help alleviate deadlock issues |
| 20 |
and ensure consistent execution order regardless of environment. The |
| 21 |
:class:`~kazoo.handlers.gevent.SequentialGeventHandler` runs a separate |
| 22 |
greenlet for each queue that processes the callbacks queued in order. The |
| 23 |
:class:`~kazoo.handlers.threading.SequentialThreadingHandler` runs a separate |
| 24 |
thread for each queue that processes the callbacks queued in order (thus the |
| 25 |
naming scheme which notes they are sequential in anticipation that there could |
| 26 |
be handlers shipped in the future which don't make this guarantee). |
| 27 |
|
| 28 |
Callbacks are queued by type, the 3 types being: |
| 29 |
|
| 30 |
1. Session events (State changes, registered listener functions) |
| 31 |
2. Watch events (Watch callbacks, DataWatch, and ChildrenWatch functions) |
| 32 |
3. Completion callbacks (Functions chained to |
| 33 |
:class:`~kazoo.interfaces.IAsyncResult` objects) |
| 34 |
|
| 35 |
This ensures that calls can be made to Zookeeper from any callback **except for |
| 36 |
a state listener** without worrying that critical session events will be |
| 37 |
blocked. |
| 38 |
|
| 39 |
.. warning:: |
| 40 |
|
| 41 |
Its important to remember that if you write code that blocks in one of |
| 42 |
these functions then no queued functions of that type will be executed |
| 43 |
until the code stops blocking. If your code might block, it should run |
| 44 |
itself in a separate greenlet/thread so that the other callbacks can |
| 45 |
run. |