[issue39483] Proposial add loop parametr to run in asyncio

Yury Selivanov report at bugs.python.org
Tue Feb 11 19:05:07 EST 2020


Yury Selivanov <yselivanov at gmail.com> added the comment:

Андрей,

Here's how you can fix your example:


import asyncio


class Singleton:
    _LOCK = None
    _LOOP = None

    @classmethod
    async def create(cls):
        if cls._LOOP is None:
            cls._LOOP = asyncio.get_running_loop()
            cls._LOCK = asyncio.Lock()
        loop = cls._LOOP
        if loop is not asyncio.get_running_loop():
            raise RuntimeError()

        if not hasattr(cls, '_Singleton__instance'):
            async with cls._LOCK:
                if not hasattr(cls, '_Singleton__instance'):
                    await asyncio.sleep(1)
                    cls.__instance = cls()
        return cls.__instance

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39483>
_______________________________________


More information about the Python-bugs-list mailing list