blob: 1e769a87976cdc469286348f2849bd8dd24d24f8 [file] [log] [blame]
Jeremy Hylton92bb6e72002-08-14 19:25:42 +00001"""Thread module emulating a subset of Java's threading model."""
Guido van Rossum7f5013a1998-04-09 22:01:42 +00002
3import sys
4import time
5import thread
6import traceback
7import StringIO
8
9# Rename some stuff so "from threading import *" is safe
10
11_sys = sys
12del sys
13
14_time = time.time
15_sleep = time.sleep
16del time
17
18_start_new_thread = thread.start_new_thread
19_allocate_lock = thread.allocate_lock
20_get_ident = thread.get_ident
Jeremy Hyltonb5fc7492000-06-01 01:17:17 +000021ThreadError = thread.error
Guido van Rossum7f5013a1998-04-09 22:01:42 +000022del thread
23
24_print_exc = traceback.print_exc
25del traceback
26
27_StringIO = StringIO.StringIO
28del StringIO
29
30
31# Debug support (adapted from ihooks.py)
32
Guido van Rossum8ca162f2002-04-07 06:36:23 +000033_VERBOSE = 0 # XXX Bool or int?
Guido van Rossum7f5013a1998-04-09 22:01:42 +000034
35if __debug__:
36
37 class _Verbose:
38
39 def __init__(self, verbose=None):
40 if verbose is None:
41 verbose = _VERBOSE
42 self.__verbose = verbose
43
44 def _note(self, format, *args):
45 if self.__verbose:
46 format = format % args
47 format = "%s: %s\n" % (
48 currentThread().getName(), format)
49 _sys.stderr.write(format)
50
51else:
52 # Disable this when using "python -O"
53 class _Verbose:
54 def __init__(self, verbose=None):
55 pass
56 def _note(self, *args):
57 pass
58
59
60# Synchronization classes
61
62Lock = _allocate_lock
63
64def RLock(*args, **kwargs):
65 return apply(_RLock, args, kwargs)
66
67class _RLock(_Verbose):
Tim Petersb90f89a2001-01-15 03:26:36 +000068
Guido van Rossum7f5013a1998-04-09 22:01:42 +000069 def __init__(self, verbose=None):
70 _Verbose.__init__(self, verbose)
71 self.__block = _allocate_lock()
72 self.__owner = None
73 self.__count = 0
74
75 def __repr__(self):
76 return "<%s(%s, %d)>" % (
77 self.__class__.__name__,
78 self.__owner and self.__owner.getName(),
79 self.__count)
80
81 def acquire(self, blocking=1):
82 me = currentThread()
83 if self.__owner is me:
84 self.__count = self.__count + 1
85 if __debug__:
86 self._note("%s.acquire(%s): recursive success", self, blocking)
87 return 1
88 rc = self.__block.acquire(blocking)
89 if rc:
90 self.__owner = me
91 self.__count = 1
92 if __debug__:
93 self._note("%s.acquire(%s): initial succes", self, blocking)
94 else:
95 if __debug__:
96 self._note("%s.acquire(%s): failure", self, blocking)
97 return rc
98
99 def release(self):
100 me = currentThread()
101 assert self.__owner is me, "release() of un-acquire()d lock"
102 self.__count = count = self.__count - 1
103 if not count:
104 self.__owner = None
105 self.__block.release()
106 if __debug__:
107 self._note("%s.release(): final release", self)
108 else:
109 if __debug__:
110 self._note("%s.release(): non-final release", self)
111
112 # Internal methods used by condition variables
113
114 def _acquire_restore(self, (count, owner)):
115 self.__block.acquire()
116 self.__count = count
117 self.__owner = owner
118 if __debug__:
119 self._note("%s._acquire_restore()", self)
120
121 def _release_save(self):
122 if __debug__:
123 self._note("%s._release_save()", self)
124 count = self.__count
125 self.__count = 0
126 owner = self.__owner
127 self.__owner = None
128 self.__block.release()
129 return (count, owner)
130
131 def _is_owned(self):
132 return self.__owner is currentThread()
133
134
135def Condition(*args, **kwargs):
136 return apply(_Condition, args, kwargs)
137
138class _Condition(_Verbose):
139
140 def __init__(self, lock=None, verbose=None):
141 _Verbose.__init__(self, verbose)
142 if lock is None:
143 lock = RLock()
144 self.__lock = lock
145 # Export the lock's acquire() and release() methods
146 self.acquire = lock.acquire
147 self.release = lock.release
148 # If the lock defines _release_save() and/or _acquire_restore(),
149 # these override the default implementations (which just call
150 # release() and acquire() on the lock). Ditto for _is_owned().
151 try:
152 self._release_save = lock._release_save
153 except AttributeError:
154 pass
155 try:
156 self._acquire_restore = lock._acquire_restore
157 except AttributeError:
158 pass
159 try:
160 self._is_owned = lock._is_owned
161 except AttributeError:
162 pass
163 self.__waiters = []
164
165 def __repr__(self):
166 return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
167
168 def _release_save(self):
169 self.__lock.release() # No state to save
170
171 def _acquire_restore(self, x):
172 self.__lock.acquire() # Ignore saved state
173
174 def _is_owned(self):
Jeremy Hylton39c12bf2002-08-14 17:46:40 +0000175 # Return True if lock is owned by currentThread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000176 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000177 if self.__lock.acquire(0):
178 self.__lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000179 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000180 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000181 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000182
183 def wait(self, timeout=None):
Jeremy Hylton29c21062002-08-14 17:56:13 +0000184 currentThread() # for side-effect
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000185 assert self._is_owned(), "wait() of un-acquire()d lock"
186 waiter = _allocate_lock()
187 waiter.acquire()
188 self.__waiters.append(waiter)
189 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000190 try: # restore state no matter what (e.g., KeyboardInterrupt)
191 if timeout is None:
192 waiter.acquire()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000193 if __debug__:
Tim Petersc951bf92001-04-02 20:15:57 +0000194 self._note("%s.wait(): got it", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000195 else:
Tim Petersa6a4f272001-08-12 00:41:33 +0000196 # Balancing act: We can't afford a pure busy loop, so we
197 # have to sleep; but if we sleep the whole timeout time,
198 # we'll be unresponsive. The scheme here sleeps very
199 # little at first, longer as time goes on, but never longer
200 # than 20 times per second (or the timeout time remaining).
Tim Petersc951bf92001-04-02 20:15:57 +0000201 endtime = _time() + timeout
Tim Petersa6a4f272001-08-12 00:41:33 +0000202 delay = 0.0005 # 500 us -> initial delay of 1 ms
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000203 while True:
Tim Petersc951bf92001-04-02 20:15:57 +0000204 gotit = waiter.acquire(0)
Tim Petersa6a4f272001-08-12 00:41:33 +0000205 if gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000206 break
Tim Petersa6a4f272001-08-12 00:41:33 +0000207 remaining = endtime - _time()
208 if remaining <= 0:
209 break
210 delay = min(delay * 2, remaining, .05)
Tim Petersc951bf92001-04-02 20:15:57 +0000211 _sleep(delay)
Tim Petersc951bf92001-04-02 20:15:57 +0000212 if not gotit:
213 if __debug__:
214 self._note("%s.wait(%s): timed out", self, timeout)
215 try:
216 self.__waiters.remove(waiter)
217 except ValueError:
218 pass
219 else:
220 if __debug__:
221 self._note("%s.wait(%s): got it", self, timeout)
222 finally:
223 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000224
225 def notify(self, n=1):
Jeremy Hylton29c21062002-08-14 17:56:13 +0000226 currentThread() # for side-effect
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000227 assert self._is_owned(), "notify() of un-acquire()d lock"
228 __waiters = self.__waiters
229 waiters = __waiters[:n]
230 if not waiters:
231 if __debug__:
232 self._note("%s.notify(): no waiters", self)
233 return
234 self._note("%s.notify(): notifying %d waiter%s", self, n,
235 n!=1 and "s" or "")
236 for waiter in waiters:
237 waiter.release()
238 try:
239 __waiters.remove(waiter)
240 except ValueError:
241 pass
242
243 def notifyAll(self):
244 self.notify(len(self.__waiters))
245
246
247def Semaphore(*args, **kwargs):
248 return apply(_Semaphore, args, kwargs)
249
250class _Semaphore(_Verbose):
251
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000252 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000253
254 def __init__(self, value=1, verbose=None):
255 assert value >= 0, "Semaphore initial value must be >= 0"
256 _Verbose.__init__(self, verbose)
257 self.__cond = Condition(Lock())
258 self.__value = value
259
260 def acquire(self, blocking=1):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000261 rc = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000262 self.__cond.acquire()
263 while self.__value == 0:
264 if not blocking:
265 break
Skip Montanarob446fc72001-08-19 04:25:24 +0000266 if __debug__:
267 self._note("%s.acquire(%s): blocked waiting, value=%s",
268 self, blocking, self.__value)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000269 self.__cond.wait()
270 else:
271 self.__value = self.__value - 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000272 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000273 self._note("%s.acquire: success, value=%s",
274 self, self.__value)
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000275 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000276 self.__cond.release()
277 return rc
278
279 def release(self):
280 self.__cond.acquire()
281 self.__value = self.__value + 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000282 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000283 self._note("%s.release: success, value=%s",
284 self, self.__value)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000285 self.__cond.notify()
286 self.__cond.release()
287
288
Skip Montanaroe428bb72001-08-20 20:27:58 +0000289def BoundedSemaphore(*args, **kwargs):
290 return apply(_BoundedSemaphore, args, kwargs)
291
292class _BoundedSemaphore(_Semaphore):
293 """Semaphore that checks that # releases is <= # acquires"""
294 def __init__(self, value=1, verbose=None):
295 _Semaphore.__init__(self, value, verbose)
296 self._initial_value = value
297
298 def release(self):
299 if self._Semaphore__value >= self._initial_value:
300 raise ValueError, "Semaphore released too many times"
301 return _Semaphore.release(self)
302
303
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000304def Event(*args, **kwargs):
305 return apply(_Event, args, kwargs)
306
307class _Event(_Verbose):
308
309 # After Tim Peters' event class (without is_posted())
310
311 def __init__(self, verbose=None):
312 _Verbose.__init__(self, verbose)
313 self.__cond = Condition(Lock())
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000314 self.__flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000315
316 def isSet(self):
317 return self.__flag
318
319 def set(self):
320 self.__cond.acquire()
Guido van Rossum21b60142002-11-21 21:08:39 +0000321 try:
322 self.__flag = True
323 self.__cond.notifyAll()
324 finally:
325 self.__cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000326
327 def clear(self):
328 self.__cond.acquire()
Guido van Rossum21b60142002-11-21 21:08:39 +0000329 try:
330 self.__flag = False
331 finally:
332 self.__cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000333
334 def wait(self, timeout=None):
335 self.__cond.acquire()
Guido van Rossum21b60142002-11-21 21:08:39 +0000336 try:
337 if not self.__flag:
338 self.__cond.wait(timeout)
339 finally:
340 self.__cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000341
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000342# Helper to generate new thread names
343_counter = 0
344def _newname(template="Thread-%d"):
345 global _counter
346 _counter = _counter + 1
347 return template % _counter
348
349# Active thread administration
350_active_limbo_lock = _allocate_lock()
351_active = {}
352_limbo = {}
353
354
355# Main class for threads
356
357class Thread(_Verbose):
358
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000359 __initialized = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000360
361 def __init__(self, group=None, target=None, name=None,
362 args=(), kwargs={}, verbose=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000363 assert group is None, "group argument must be None for now"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000364 _Verbose.__init__(self, verbose)
365 self.__target = target
366 self.__name = str(name or _newname())
367 self.__args = args
368 self.__kwargs = kwargs
369 self.__daemonic = self._set_daemon()
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000370 self.__started = False
371 self.__stopped = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000372 self.__block = Condition(Lock())
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000373 self.__initialized = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000374
375 def _set_daemon(self):
376 # Overridden in _MainThread and _DummyThread
377 return currentThread().isDaemon()
378
379 def __repr__(self):
380 assert self.__initialized, "Thread.__init__() was not called"
381 status = "initial"
382 if self.__started:
383 status = "started"
384 if self.__stopped:
385 status = "stopped"
386 if self.__daemonic:
387 status = status + " daemon"
388 return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status)
389
390 def start(self):
391 assert self.__initialized, "Thread.__init__() not called"
392 assert not self.__started, "thread already started"
393 if __debug__:
394 self._note("%s.start(): starting thread", self)
395 _active_limbo_lock.acquire()
396 _limbo[self] = self
397 _active_limbo_lock.release()
398 _start_new_thread(self.__bootstrap, ())
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000399 self.__started = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000400 _sleep(0.000001) # 1 usec, to let the thread run (Solaris hack)
401
402 def run(self):
403 if self.__target:
404 apply(self.__target, self.__args, self.__kwargs)
405
406 def __bootstrap(self):
407 try:
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000408 self.__started = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000409 _active_limbo_lock.acquire()
410 _active[_get_ident()] = self
411 del _limbo[self]
412 _active_limbo_lock.release()
413 if __debug__:
414 self._note("%s.__bootstrap(): thread started", self)
415 try:
416 self.run()
417 except SystemExit:
418 if __debug__:
419 self._note("%s.__bootstrap(): raised SystemExit", self)
420 except:
421 if __debug__:
422 self._note("%s.__bootstrap(): unhandled exception", self)
423 s = _StringIO()
424 _print_exc(file=s)
425 _sys.stderr.write("Exception in thread %s:\n%s\n" %
426 (self.getName(), s.getvalue()))
427 else:
428 if __debug__:
429 self._note("%s.__bootstrap(): normal return", self)
430 finally:
431 self.__stop()
Guido van Rossumf21b2aa2001-12-28 22:07:09 +0000432 try:
433 self.__delete()
434 except:
435 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000436
437 def __stop(self):
438 self.__block.acquire()
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000439 self.__stopped = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000440 self.__block.notifyAll()
441 self.__block.release()
442
443 def __delete(self):
444 _active_limbo_lock.acquire()
445 del _active[_get_ident()]
446 _active_limbo_lock.release()
447
448 def join(self, timeout=None):
449 assert self.__initialized, "Thread.__init__() not called"
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000450 assert self.__started, "cannot join thread before it is started"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000451 assert self is not currentThread(), "cannot join current thread"
452 if __debug__:
453 if not self.__stopped:
454 self._note("%s.join(): waiting until thread stops", self)
455 self.__block.acquire()
456 if timeout is None:
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000457 while not self.__stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000458 self.__block.wait()
459 if __debug__:
460 self._note("%s.join(): thread stopped", self)
461 else:
Guido van Rossumb39e4611998-05-29 17:47:10 +0000462 deadline = _time() + timeout
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000463 while not self.__stopped:
Guido van Rossumb39e4611998-05-29 17:47:10 +0000464 delay = deadline - _time()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000465 if delay <= 0:
466 if __debug__:
467 self._note("%s.join(): timed out", self)
468 break
469 self.__block.wait(delay)
470 else:
471 if __debug__:
472 self._note("%s.join(): thread stopped", self)
473 self.__block.release()
474
475 def getName(self):
476 assert self.__initialized, "Thread.__init__() not called"
477 return self.__name
478
479 def setName(self, name):
480 assert self.__initialized, "Thread.__init__() not called"
481 self.__name = str(name)
482
483 def isAlive(self):
484 assert self.__initialized, "Thread.__init__() not called"
485 return self.__started and not self.__stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000486
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000487 def isDaemon(self):
488 assert self.__initialized, "Thread.__init__() not called"
489 return self.__daemonic
490
491 def setDaemon(self, daemonic):
492 assert self.__initialized, "Thread.__init__() not called"
493 assert not self.__started, "cannot set daemon status of active thread"
494 self.__daemonic = daemonic
495
Martin v. Löwis44f86962001-09-05 13:44:54 +0000496# The timer class was contributed by Itamar Shtull-Trauring
497
498def Timer(*args, **kwargs):
499 return _Timer(*args, **kwargs)
500
501class _Timer(Thread):
502 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000503
Martin v. Löwis44f86962001-09-05 13:44:54 +0000504 t = Timer(30.0, f, args=[], kwargs={})
505 t.start()
506 t.cancel() # stop the timer's action if it's still waiting
507 """
Tim Petersb64bec32001-09-18 02:26:39 +0000508
Martin v. Löwis44f86962001-09-05 13:44:54 +0000509 def __init__(self, interval, function, args=[], kwargs={}):
510 Thread.__init__(self)
511 self.interval = interval
512 self.function = function
513 self.args = args
514 self.kwargs = kwargs
515 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000516
Martin v. Löwis44f86962001-09-05 13:44:54 +0000517 def cancel(self):
518 """Stop the timer if it hasn't finished yet"""
519 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000520
Martin v. Löwis44f86962001-09-05 13:44:54 +0000521 def run(self):
522 self.finished.wait(self.interval)
523 if not self.finished.isSet():
524 self.function(*self.args, **self.kwargs)
525 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000526
527# Special thread class to represent the main thread
528# This is garbage collected through an exit handler
529
530class _MainThread(Thread):
531
532 def __init__(self):
533 Thread.__init__(self, name="MainThread")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000534 self._Thread__started = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000535 _active_limbo_lock.acquire()
536 _active[_get_ident()] = self
537 _active_limbo_lock.release()
Fred Drake7b4fc172000-08-18 15:50:54 +0000538 import atexit
539 atexit.register(self.__exitfunc)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000540
541 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000542 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000543
544 def __exitfunc(self):
545 self._Thread__stop()
546 t = _pickSomeNonDaemonThread()
547 if t:
548 if __debug__:
549 self._note("%s: waiting for other threads", self)
550 while t:
551 t.join()
552 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000553 if __debug__:
554 self._note("%s: exiting", self)
555 self._Thread__delete()
556
557def _pickSomeNonDaemonThread():
558 for t in enumerate():
559 if not t.isDaemon() and t.isAlive():
560 return t
561 return None
562
563
564# Dummy thread class to represent threads not started here.
565# These aren't garbage collected when they die,
566# nor can they be waited for.
567# Their purpose is to return *something* from currentThread().
568# They are marked as daemon threads so we won't wait for them
569# when we exit (conform previous semantics).
570
571class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000572
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000573 def __init__(self):
574 Thread.__init__(self, name=_newname("Dummy-%d"))
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000575 self._Thread__started = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000576 _active_limbo_lock.acquire()
577 _active[_get_ident()] = self
578 _active_limbo_lock.release()
579
580 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000581 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000582
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000583 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000584 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000585
586
587# Global API functions
588
589def currentThread():
590 try:
591 return _active[_get_ident()]
592 except KeyError:
Guido van Rossum5080b332000-12-15 20:08:39 +0000593 ##print "currentThread(): no current thread for", _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000594 return _DummyThread()
595
596def activeCount():
597 _active_limbo_lock.acquire()
598 count = len(_active) + len(_limbo)
599 _active_limbo_lock.release()
600 return count
601
602def enumerate():
603 _active_limbo_lock.acquire()
604 active = _active.values() + _limbo.values()
605 _active_limbo_lock.release()
606 return active
607
608
609# Create the main thread object
610
611_MainThread()
612
613
614# Self-test code
615
616def _test():
617
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000618 class BoundedQueue(_Verbose):
619
620 def __init__(self, limit):
621 _Verbose.__init__(self)
622 self.mon = RLock()
623 self.rc = Condition(self.mon)
624 self.wc = Condition(self.mon)
625 self.limit = limit
626 self.queue = []
627
628 def put(self, item):
629 self.mon.acquire()
630 while len(self.queue) >= self.limit:
631 self._note("put(%s): queue full", item)
632 self.wc.wait()
633 self.queue.append(item)
634 self._note("put(%s): appended, length now %d",
635 item, len(self.queue))
636 self.rc.notify()
637 self.mon.release()
638
639 def get(self):
640 self.mon.acquire()
641 while not self.queue:
642 self._note("get(): queue empty")
643 self.rc.wait()
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000644 item = self.queue.pop(0)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000645 self._note("get(): got %s, %d left", item, len(self.queue))
646 self.wc.notify()
647 self.mon.release()
648 return item
649
650 class ProducerThread(Thread):
651
652 def __init__(self, queue, quota):
653 Thread.__init__(self, name="Producer")
654 self.queue = queue
655 self.quota = quota
656
657 def run(self):
Guido van Rossumb26a1b41998-05-20 17:05:52 +0000658 from random import random
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000659 counter = 0
660 while counter < self.quota:
661 counter = counter + 1
662 self.queue.put("%s.%d" % (self.getName(), counter))
663 _sleep(random() * 0.00001)
664
665
666 class ConsumerThread(Thread):
667
668 def __init__(self, queue, count):
669 Thread.__init__(self, name="Consumer")
670 self.queue = queue
671 self.count = count
672
673 def run(self):
674 while self.count > 0:
675 item = self.queue.get()
676 print item
677 self.count = self.count - 1
678
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000679 NP = 3
680 QL = 4
681 NI = 5
682
683 Q = BoundedQueue(QL)
684 P = []
685 for i in range(NP):
686 t = ProducerThread(Q, NI)
687 t.setName("Producer-%d" % (i+1))
688 P.append(t)
689 C = ConsumerThread(Q, NI*NP)
690 for t in P:
691 t.start()
692 _sleep(0.000001)
693 C.start()
694 for t in P:
695 t.join()
696 C.join()
697
698if __name__ == '__main__':
699 _test()