[issue39483] Proposial add loop parametr to run in asyncio

Андрей Казанцев report at bugs.python.org
Wed Jan 29 14:49:18 EST 2020


Андрей Казанцев <heckad at yandex.ru> added the comment:

How to make singleton class? I wrote this 

```
import asyncio


class Singleton:
    _CREATE_LOCK = asyncio.Lock()

    @classmethod
    async def create(cls):
        if not hasattr(cls, '_Singleton__instance'):
            async with cls._CREATE_LOCK:
                if not hasattr(cls, '_Singleton__instance'):
                    await asyncio.sleep(1)
                    cls.__instance = cls()
        return cls.__instance


async def main():
    await asyncio.wait([
        Singleton.create(),
        Singleton.create(),
    ])


if __name__ == '__main__':
    asyncio.run(main())
```

and got `RuntimeError`

----------

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


More information about the Python-bugs-list mailing list