How to use the ring.func.asyncio function in ring

To help you get started, we’ve selected a few ring 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 youknowone / ring / tests / _test_func_asyncio.py View on Github external
def aiomcache_client():
    client = AiomcacheProxy()
    return client, ring.func.asyncio.aiomcache
github youknowone / ring / ring / func / asyncio.py View on Github external
def get_or_update_many(self, wire, *args_list):
        keys = self.key_many(wire, *args_list)
        miss_value = object()
        results = yield from wire.storage.get_many(
            keys, miss_value=miss_value)

        miss_indices = []
        for i, akr in enumerate(zip(args_list, keys, results)):
            args, key, result = akr
            if result is not miss_value:
                continue
            miss_indices.append(i)

        new_results = yield from asyncio.gather(*(
            fbase.execute_bulk_item(wire, args_list[i]) for i in miss_indices))
        new_keys = [keys[i] for i in miss_indices]
        yield from wire.storage.set_many(new_keys, new_results)

        for new_i, old_i in enumerate(miss_indices):
            results[old_i] = new_results[new_i]
        return results
github youknowone / ring / ring / func / __init__.py View on Github external
try:
    import asyncio as _has_asyncio
except ImportError:
    _has_asyncio = False
else:
    from ring.func import asyncio


__all__ = (
    'lru', 'dict', 'memcache', 'redis', 'redis_hash', 'shelve', 'disk')


if _has_asyncio:
    lru = asyncio.create_asyncio_factory_proxy(
        (sync.lru, asyncio.create_factory_from(sync.lru, sync.LruStorage)),
        support_asyncio=False)
    dict = asyncio.create_asyncio_factory_proxy(
        (sync.dict, asyncio.dict),
        support_asyncio=True)
    shelve = asyncio.create_asyncio_factory_proxy(
        (sync.shelve, asyncio.create_factory_from(sync.shelve, sync.ShelveStorage)),
        support_asyncio=False)
    disk = asyncio.create_asyncio_factory_proxy(
        (sync.diskcache, asyncio.create_factory_from(sync.diskcache, sync.DiskCacheStorage)),
        support_asyncio=False)
    memcache = asyncio.create_asyncio_factory_proxy(
        (sync.memcache, asyncio.aiomcache),
        support_asyncio=True)
    redis = asyncio.create_asyncio_factory_proxy(
        (sync.redis_py, asyncio.aioredis),
        support_asyncio=True)
github youknowone / ring / ring / func / asyncio.py View on Github external
def convert_storage(storage_class):
    storage_bases = (fbase.CommonMixinStorage, BulkStorageMixin)
    async_storage_class = type(
        'Async' + storage_class.__name__, (storage_class,), {})

    count = 0
    for storage_base in storage_bases:
        if issubclass(storage_class, storage_base):
            count += 1
            for name in storage_base.__dict__.keys():
                async_attr = asyncio.coroutine(getattr(storage_class, name))
                setattr(async_storage_class, name, async_attr)
    if count == 0:
        raise TypeError(
            "'storage_class' is not subclassing any known storage base")

    return async_storage_class
github youknowone / ring / ring / func / asyncio.py View on Github external
Though they have slightly different behavior for `.storage.backend`:

            >>> assert by_object.storage.backend is by_object

            >>> assert by_coroutine.storage.backend is not redis_coroutine
            >>> assert isinstance(
            ...     await by_coroutine.storage.backend, aioredis.Redis)

    :see: :func:`ring.func.asyncio.CacheUserInterface` for single access
        sub-functions.
    :see: :func:`ring.func.asyncio.BulkInterfaceMixin` for bulk access
        sub-functions.

    :see: :func:`ring.redis` for non-asyncio version.
    """
    if asyncio.iscoroutine(redis):
        redis = SingletonCoroutineProxy(redis)

    return fbase.factory(
        redis, key_prefix=key_prefix, on_manufactured=factory_doctor,
        user_interface=user_interface, storage_class=storage_class,
        miss_value=None, expire_default=expire, coder=coder,
        **kwargs)
github youknowone / ring / ring / func / asyncio.py View on Github external
def __init__(self, awaitable):
        if not asyncio.iscoroutine(awaitable):
            raise TypeError(
                "StorageProxy requires an awaitable object but '{}' found"
                .format(type(awaitable)))
        self.awaitable = awaitable
        self.singleton = None
github youknowone / ring / ring / func / asyncio.py View on Github external
def set_many_values(self, keys, values, expire):
        params = itertools.chain.from_iterable(zip(keys, values))
        backend = yield from self._get_backend()
        yield from backend.mset(*params)
        if expire is not None:
            asyncio.ensure_future(asyncio.gather(*(
                backend.expire(key, expire) for key in keys)))