Skip to content

Instantly share code, notes, and snippets.

@JobsDong
Created December 4, 2015 03:42
Show Gist options
  • Save JobsDong/7ee0c1df92658840602d to your computer and use it in GitHub Desktop.
Save JobsDong/7ee0c1df92658840602d to your computer and use it in GitHub Desktop.
python singleton
#!/usr/bin/python
# -*- coding: utf-8 -*-
import threading
class KbTerms(object):
"""singleton
"""
_instance_lock = threading.Lock()
@staticmethod
def instance():
if not hasattr(KbTerms, "_instance"):
with KbTerms._instance_lock:
if not hasattr(KbTerms, "_instance"):
setattr(KbTerms, "_instance", KbTerms())
return getattr(KbTerms, "_instance")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment