Index: gft_common.py |
diff --git a/gft_common.py b/gft_common.py |
index 1fbbb237cfc3c3a433fd436cbe00188b5edd165f..1c65c464f2433b5612152f1f7d8f86fd9da8afdd 100644 |
--- a/gft_common.py |
+++ b/gft_common.py |
@@ -14,6 +14,7 @@ import os |
import subprocess |
import sys |
import tempfile |
+import threading |
import time |
@@ -214,6 +215,32 @@ def WriteFile(filename, data): |
opened_file.write(data) |
+def ThreadSafe(f): |
+ """ Decorator for functions that need synchronoization. """ |
+ lock = threading.Lock() |
+ def threadsafe_call(*args): |
+ try: |
+ lock.acquire() |
+ return f(*args) |
+ finally: |
+ lock.release() |
+ return threadsafe_call |
+ |
+ |
+def Memorize(f): |
+ """ Decorator for functions that need memorization. """ |
+ memorize_data = {} |
+ def memorize_call(*args): |
+ index = repr(args) |
+ if index in memorize_data: |
+ value = memorize_data[index] |
+ # DebugMsg('Memorize: using cached value for: %s %s' % (repr(f), index)) |
+ return value |
+ value = f(*args) |
+ memorize_data[index] = value |
+ return value |
+ return memorize_call |
+ |
######################################################################## |
# Components Databases |