Skip to content

Commit

Permalink
Merge pull request #1 from redshiftzero/multithread-singleton
Browse files Browse the repository at this point in the history
synchronize singleton creation using threading.Lock() in __call__
  • Loading branch information
kushaldas authored Dec 2, 2019
2 parents 5c81deb + 096a57d commit 667e8f1
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions oqubeslogging.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from logging import StreamHandler
from subprocess import Popen, PIPE
import threading


class Singleton(type):
_ins = {}
_lock = threading.Lock()

def __call__(cls, *args, **kwargs):
if cls not in cls._ins:
cls._ins[cls] = (super(Singleton, cls).__call__(*args, **kwargs), args)
with cls._lock: # First thread that gets here creates the instance
if cls not in cls._ins:
cls._ins[cls] = (super(Singleton, cls).__call__(*args, **kwargs), args)

if len(args) > 1:
if args != cls._ins[cls][1]:
Expand Down

0 comments on commit 667e8f1

Please sign in to comment.