How to use the aiozk.recipes.sequential.SequentialRecipe function in aiozk

To help you get started, we’ve selected a few aiozk 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 micro-fan / aiozk / aiozk / recipes / double_barrier.py View on Github external
import asyncio
import logging

from aiozk import exc, WatchEvent

from .sequential import SequentialRecipe

log = logging.getLogger(__name__)


class DoubleBarrier(SequentialRecipe):

    def __init__(self, base_path, min_participants):
        super().__init__(base_path)
        self.min_participants = min_participants

    @property
    def sentinel_path(self):
        return self.sibling_path("sentinel")

    async def enter(self, timeout=None):
        log.debug("Entering double barrier %s", self.base_path)
        barrier_lifted = self.client.wait_for_events(
            [WatchEvent.CREATED], self.sentinel_path
        )

        exists = await self.client.exists(path=self.sentinel_path, watch=True)
github micro-fan / aiozk / aiozk / recipes / lease.py View on Github external
import asyncio
import logging
import time

from aiozk import exc

from .sequential import SequentialRecipe
from functools import partial

log = logging.getLogger(__name__)


class Lease(SequentialRecipe):

    def __init__(self, base_path, limit=1):
        super().__init__(base_path)
        self.limit = limit

    async def obtain(self, duration):
        lessees = await self.client.get_children(self.base_path)

        if len(lessees) >= self.limit:
            return False

        time_limit = time.time() + duration.total_seconds()

        try:
            await self.create_unique_znode("lease", data=str(time_limit))
        except exc.NodeExists:
github micro-fan / aiozk / aiozk / recipes / base_lock.py View on Github external
import asyncio
import logging
import time

from aiozk import exc, states

from .sequential import SequentialRecipe

log = logging.getLogger(__name__)


class BaseLock(SequentialRecipe):
    async def wait_in_line(self, znode_label, timeout=None, blocked_by=None):
        time_limit = None
        if timeout is not None:
            time_limit = time.time() + timeout

        await self.create_unique_znode(znode_label)

        while True:
            if time_limit and time.time() >= time_limit:
                await self.delete_unique_znode(znode_label)
                raise exc.TimeoutError

            owned_positions, contenders = await self.analyze_siblings()
            if znode_label not in owned_positions:
                raise exc.SessionLost
github micro-fan / aiozk / aiozk / recipes / election.py View on Github external
import asyncio

from .sequential import SequentialRecipe


class LeaderElection(SequentialRecipe):

    def __init__(self, base_path):
        super().__init__(base_path)
        self.has_leadership = False

        self.leadership_future = self.client.loop.create_future()

    async def join(self):
        await self.create_unique_znode("candidate")
        await self.check_position()

    async def check_position(self, _=None):
        owned_positions, candidates = await self.analyze_siblings()
        if "candidate" not in owned_positions:
            return
github micro-fan / aiozk / aiozk / recipes / party.py View on Github external
from .children_watcher import ChildrenWatcher
from .sequential import SequentialRecipe


class Party(SequentialRecipe):

    sub_recipes = {
        "watcher": ChildrenWatcher,
    }

    def __init__(self, base_path, name):
        super().__init__(base_path)

        self.name = name
        self.members = []
        self.change_future = None

    async def join(self):
        await self.create_unique_znode(self.name)
        _, siblings = await self.analyze_siblings()
        self.update_members(siblings)