This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author yselivanov
Recipients asvetlov, cjrh, heckad, yselivanov
Date 2020-02-12.00:05:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1581465907.11.0.0885918685453.issue39483@roundup.psfhosted.org>
In-reply-to
Content
Андрей,

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
History
Date User Action Args
2020-02-12 00:05:07yselivanovsetrecipients: + yselivanov, asvetlov, cjrh, heckad
2020-02-12 00:05:07yselivanovsetmessageid: <1581465907.11.0.0885918685453.issue39483@roundup.psfhosted.org>
2020-02-12 00:05:07yselivanovlinkissue39483 messages
2020-02-12 00:05:07yselivanovcreate