Андрей Казанцев <hec...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue39483>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to