From 1a80fc5a0e093c5072263b3c3b991538619510cd Mon Sep 17 00:00:00 2001 From: the31k Date: Wed, 19 Jul 2017 01:35:19 +0300 Subject: [PATCH] Per-thread singletons --- telebot/util.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/telebot/util.py b/telebot/util.py index f524577..ab7650d 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -17,6 +17,9 @@ except ImportError: from telebot import logger +thread_local = threading.local() + + class WorkerThread(threading.Thread): count = 0 @@ -242,3 +245,12 @@ def extract_arguments(text): regexp = re.compile("\/\w*(@\w*)*\s*([\s\S]*)",re.IGNORECASE) result = regexp.match(text) return result.group(2) if is_command(text) else None + + +def per_thread(key, construct_value): + try: + return getattr(thread_local, key) + except AttributeError: + value = construct_value() + setattr(thread_local, key, value) + return value