1
2
3
4 """
5 Threading.
6
7 @var stoppable_server_check_interval: The time interval (in seconds)
8 between checks by L{servers.StoppableServerMixin}s as to whether they
9 should stop.
10 """
11
12 from decs import *
13 from log import *
14 import functools, threading
15
16
17 stoppable_server_check_interval = 1
18
19 -class Thread( threading.Thread ):
20 """
21 Adds extra features to the Thread class, including a way to
22 request termination.
23 """
24
25 _lock = threading.Lock()
26 _stopEvent = threading.Event()
27 _doStop = False
28
30 """
31 Initializes the termination event.
32 """
33 threading.Thread.__init__( self )
34
35 @classmethod
44
45 @classmethod
55
69
73 """
74 Creates and starts a thread.
75 """
76 debug( 'commons.spawn_thread', 'spawning thread' )
77 thread = threading.Thread( target = func, args = args, kwargs = kwargs )
78 thread.start()
79 return thread
80
84 """
85 Wrap an object and all of its methods with synchronization.
86
87 Example::
88
89 class SynchronizedObject(GenericWrapper):
90 ''' wrap an object and all of its methods with synchronization '''
91 def _ _init_ _(self, obj, ignore=( ), lock=None):
92 if lock is None:
93 import threading
94 lock = threading.RLock( )
95 GenericWrapper._ _init_ _(self, obj, lock.acquire, lock.release, ignore)
96
97 From the Python Cookbook.
98
99 @copyright: O'Reilly Media
100 """
101 - def __init__(self, obj, ignore=( ), lock=None):
102 if lock is None:
103 import threading
104 lock = threading.RLock( )
105 GenericWrapper.__init__(self, obj, lock.acquire, lock.release, ignore)
106
110 '''
111 Synchronized methods.
112
113 From U{http://www.ddj.com/184406073}.
114
115 @copyright: Phillip Eby
116
117 Example::
118
119 class SomeClass:
120 """Example usage"""
121 @synchronized
122 def doSomething(self,someParam):
123 """This method can only be entered
124 by one thread at a time"""
125 '''
126 @functools.wraps(func)
127 def wrapper(self,*__args,**__kw):
128 try:
129 rlock = self._sync_lock
130 except AttributeError:
131
132
133 rlock = self.__dict__.setdefault( '_sync_lock',
134 threading.RLock() )
135 rlock.acquire()
136 try:
137 return func(self,*__args,**__kw)
138 finally:
139 rlock.release()
140 return wrapper
141
143 """
144 This is useful for functions which aren't methods or when we want
145 to synchronize on something other than C{self} (and note that we
146 can synchronize on multiple locks).
147 """
148 def synchronized(func):
149 @functools.wraps(func)
150 def wrapper(self,*__args,**__kw):
151 for rlock in rlocks:
152 rlock.acquire()
153 try:
154 return func(self,*__args,**__kw)
155 finally:
156 for rlock in rlocks:
157 rlock.release()
158 return wrapper
159 return synchronized
160