ocarina/libsaria/threads.py
Bryan Schumaker b562fff0ed libsaria: Began implementing better callback system
The event system was evil, hard to use, and hard to follow.  This patch
is the beginning of a new, more straightforward system.
2011-05-01 12:46:16 -04:00

44 lines
790 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):
print "running func %s with args %s" % (self.func, self.args)
self.func(*self.args)
print "exiting func %s" % self.func
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