Skip to content

Instantly share code, notes, and snippets.

View CorentinJ's full-sized avatar

Corentin Jemine CorentinJ

View GitHub Profile
@sodastsai
sodastsai / singleton.py
Last active September 23, 2019 22:53
Python singleton metaclass (thread-safe)
from __future__ import absolute_import, division, print_function, unicode_literals
from multiprocessing.dummy import Pool as ThreadPool
from multiprocessing import Lock
from threading import get_ident
class SingletonType(type):
def __new__(mcs, name, bases, attrs):
# Assume the target class is created (i.e. this method to be called) in the main thread.