ocarina/libsaria/threads.py

42 lines
688 B
Python

# Bryan Schumaker (8/19/2010)
import threading
Thread = threading.Thread
Mutex = threading.Lock
mutex_mutex = Mutex()
mutexes = dict()
class BG_Thread(Thread):
def __init__(self, func, *args):
Thread.__init__(self)
self.func = func
self.args = args
def run(self):
self.func(*self.args)
def background(func, *args):
try:
th = BG_Thread(func, *args)
th.start()
return th
except Exception,e:
print e
return None
def get_mutex(lock_name):
global mutexes
global Mutex
global mutex_mutex
mutex_mutex.acquire()
lock = mutexes.get(lock_name, None)
if lock == None:
lock = Mutex()
mutexes[lock_name] = lock
mutex_mutex.release()
return lock