annotate Lib/threading.py @ 17581:c1937faabb23 2.1

Added support for optional explanation parameter to the \versionadded macro. Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most of the work in a helper function, with the do_cmd_*() wrappers just supplying a portion of the replacement text.
author Fred Drake <fdrake@acm.org>
date Wed, 18 Apr 2001 17:29:14 +0000
parents 836cdf8d1245
children a1c328337c8e 2967ca96d07e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11344
diff changeset
1 """Proposed new threading module, emulating a subset of Java's threading model."""
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 import sys
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 import time
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import thread
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import traceback
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 import StringIO
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # Rename some stuff so "from threading import *" is safe
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 _sys = sys
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 del sys
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 _time = time.time
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 _sleep = time.sleep
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 del time
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 _start_new_thread = thread.start_new_thread
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 _allocate_lock = thread.allocate_lock
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 _get_ident = thread.get_ident
12813
f1f9c30a4156 patch from Charles Waldman--
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11876
diff changeset
21 ThreadError = thread.error
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 del thread
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 _print_exc = traceback.print_exc
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 del traceback
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 _StringIO = StringIO.StringIO
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 del StringIO
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # Debug support (adapted from ihooks.py)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 _VERBOSE = 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 class _Verbose:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 def __init__(self, verbose=None):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 if verbose is None:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 verbose = _VERBOSE
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 self.__verbose = verbose
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 def _note(self, format, *args):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 if self.__verbose:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 format = format % args
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 format = "%s: %s\n" % (
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 currentThread().getName(), format)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 _sys.stderr.write(format)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 # Disable this when using "python -O"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 class _Verbose:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 def __init__(self, verbose=None):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 pass
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 def _note(self, *args):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 pass
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 # Synchronization classes
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 Lock = _allocate_lock
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 def RLock(*args, **kwargs):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 return apply(_RLock, args, kwargs)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 class _RLock(_Verbose):
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15586
diff changeset
68
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 def __init__(self, verbose=None):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 _Verbose.__init__(self, verbose)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 self.__block = _allocate_lock()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 self.__owner = None
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 self.__count = 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 def __repr__(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 return "<%s(%s, %d)>" % (
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 self.__class__.__name__,
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 self.__owner and self.__owner.getName(),
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 self.__count)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 def acquire(self, blocking=1):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 me = currentThread()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 if self.__owner is me:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 self.__count = self.__count + 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 self._note("%s.acquire(%s): recursive success", self, blocking)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 return 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 rc = self.__block.acquire(blocking)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 if rc:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 self.__owner = me
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 self.__count = 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 self._note("%s.acquire(%s): initial succes", self, blocking)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 self._note("%s.acquire(%s): failure", self, blocking)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 return rc
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 def release(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 me = currentThread()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 assert self.__owner is me, "release() of un-acquire()d lock"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 self.__count = count = self.__count - 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 if not count:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 self.__owner = None
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 self.__block.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 self._note("%s.release(): final release", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 self._note("%s.release(): non-final release", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 # Internal methods used by condition variables
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 def _acquire_restore(self, (count, owner)):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 self.__block.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 self.__count = count
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 self.__owner = owner
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 self._note("%s._acquire_restore()", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 def _release_save(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 self._note("%s._release_save()", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 count = self.__count
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 self.__count = 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 owner = self.__owner
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 self.__owner = None
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 self.__block.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 return (count, owner)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 def _is_owned(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 return self.__owner is currentThread()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 def Condition(*args, **kwargs):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 return apply(_Condition, args, kwargs)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 class _Condition(_Verbose):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 def __init__(self, lock=None, verbose=None):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 _Verbose.__init__(self, verbose)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 if lock is None:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 lock = RLock()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 self.__lock = lock
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 # Export the lock's acquire() and release() methods
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 self.acquire = lock.acquire
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 self.release = lock.release
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 # If the lock defines _release_save() and/or _acquire_restore(),
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 # these override the default implementations (which just call
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 # release() and acquire() on the lock). Ditto for _is_owned().
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 self._release_save = lock._release_save
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 except AttributeError:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 pass
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 self._acquire_restore = lock._acquire_restore
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 except AttributeError:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 pass
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 self._is_owned = lock._is_owned
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 except AttributeError:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 pass
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 self.__waiters = []
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 def __repr__(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 def _release_save(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 self.__lock.release() # No state to save
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 def _acquire_restore(self, x):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 self.__lock.acquire() # Ignore saved state
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 def _is_owned(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 if self.__lock.acquire(0):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 self.__lock.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 return 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 return 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 def wait(self, timeout=None):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 me = currentThread()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 assert self._is_owned(), "wait() of un-acquire()d lock"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 waiter = _allocate_lock()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 waiter.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 self.__waiters.append(waiter)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 saved_state = self._release_save()
17303
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
188 try: # restore state no matter what (e.g., KeyboardInterrupt)
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
189 if timeout is None:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
190 waiter.acquire()
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 if __debug__:
17303
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
192 self._note("%s.wait(): got it", self)
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 else:
17303
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
194 endtime = _time() + timeout
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
195 delay = 0.000001 # 1 usec
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
196 while 1:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
197 gotit = waiter.acquire(0)
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
198 if gotit or _time() >= endtime:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
199 break
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
200 _sleep(delay)
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
201 if delay < 1.0:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
202 delay = delay * 2.0
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
203 if not gotit:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
204 if __debug__:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
205 self._note("%s.wait(%s): timed out", self, timeout)
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
206 try:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
207 self.__waiters.remove(waiter)
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
208 except ValueError:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
209 pass
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
210 else:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
211 if __debug__:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
212 self._note("%s.wait(%s): got it", self, timeout)
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
213 finally:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
214 self._acquire_restore(saved_state)
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 def notify(self, n=1):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 me = currentThread()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 assert self._is_owned(), "notify() of un-acquire()d lock"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 __waiters = self.__waiters
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 waiters = __waiters[:n]
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 if not waiters:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 self._note("%s.notify(): no waiters", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 return
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 self._note("%s.notify(): notifying %d waiter%s", self, n,
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 n!=1 and "s" or "")
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 for waiter in waiters:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 waiter.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 __waiters.remove(waiter)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 except ValueError:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 pass
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 def notifyAll(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 self.notify(len(self.__waiters))
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 def Semaphore(*args, **kwargs):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 return apply(_Semaphore, args, kwargs)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 class _Semaphore(_Verbose):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242
11876
c27d3e8c45c4 Fix a typo in a comment
Andrew M. Kuchling <amk@amk.ca>
parents: 11707
diff changeset
243 # After Tim Peters' semaphore class, but not quite the same (no maximum)
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 def __init__(self, value=1, verbose=None):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 assert value >= 0, "Semaphore initial value must be >= 0"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 _Verbose.__init__(self, verbose)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 self.__cond = Condition(Lock())
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 self.__value = value
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 def acquire(self, blocking=1):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 rc = 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 self.__cond.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 while self.__value == 0:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 if not blocking:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 break
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 self.__cond.wait()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 self.__value = self.__value - 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 rc = 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 self.__cond.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 return rc
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
263
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 def release(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 self.__cond.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 self.__value = self.__value + 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 self.__cond.notify()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 self.__cond.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 def Event(*args, **kwargs):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 return apply(_Event, args, kwargs)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 class _Event(_Verbose):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
275
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 # After Tim Peters' event class (without is_posted())
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 def __init__(self, verbose=None):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 _Verbose.__init__(self, verbose)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 self.__cond = Condition(Lock())
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 self.__flag = 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 def isSet(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 return self.__flag
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 def set(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 self.__cond.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 self.__flag = 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 self.__cond.notifyAll()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 self.__cond.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 def clear(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 self.__cond.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 self.__flag = 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 self.__cond.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 def wait(self, timeout=None):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 self.__cond.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 if not self.__flag:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 self.__cond.wait(timeout)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 self.__cond.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
302
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
303
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 # Helper to generate new thread names
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 _counter = 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 def _newname(template="Thread-%d"):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 global _counter
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 _counter = _counter + 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 return template % _counter
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 # Active thread administration
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 _active_limbo_lock = _allocate_lock()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 _active = {}
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 _limbo = {}
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 # Main class for threads
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 class Thread(_Verbose):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
320
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 __initialized = 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
322
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 def __init__(self, group=None, target=None, name=None,
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 args=(), kwargs={}, verbose=None):
8474
049aa3a8a172 Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents: 8454
diff changeset
325 assert group is None, "group argument must be None for now"
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 _Verbose.__init__(self, verbose)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 self.__target = target
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 self.__name = str(name or _newname())
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 self.__args = args
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 self.__kwargs = kwargs
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 self.__daemonic = self._set_daemon()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 self.__started = 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 self.__stopped = 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 self.__block = Condition(Lock())
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 self.__initialized = 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
336
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 def _set_daemon(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 # Overridden in _MainThread and _DummyThread
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 return currentThread().isDaemon()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
340
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 def __repr__(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 assert self.__initialized, "Thread.__init__() was not called"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 status = "initial"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 if self.__started:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 status = "started"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 if self.__stopped:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 status = "stopped"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 if self.__daemonic:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 status = status + " daemon"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 def start(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 assert self.__initialized, "Thread.__init__() not called"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 assert not self.__started, "thread already started"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 self._note("%s.start(): starting thread", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 _active_limbo_lock.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 _limbo[self] = self
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 _active_limbo_lock.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 _start_new_thread(self.__bootstrap, ())
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 self.__started = 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 _sleep(0.000001) # 1 usec, to let the thread run (Solaris hack)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 def run(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 if self.__target:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 apply(self.__target, self.__args, self.__kwargs)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 def __bootstrap(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 self.__started = 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 _active_limbo_lock.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 _active[_get_ident()] = self
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 del _limbo[self]
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 _active_limbo_lock.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 self._note("%s.__bootstrap(): thread started", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 self.run()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 except SystemExit:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 self._note("%s.__bootstrap(): raised SystemExit", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 except:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 self._note("%s.__bootstrap(): unhandled exception", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 s = _StringIO()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 _print_exc(file=s)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 _sys.stderr.write("Exception in thread %s:\n%s\n" %
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 (self.getName(), s.getvalue()))
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 self._note("%s.__bootstrap(): normal return", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 finally:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 self.__stop()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 self.__delete()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 def __stop(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 self.__block.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 self.__stopped = 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 self.__block.notifyAll()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 self.__block.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
401
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 def __delete(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 _active_limbo_lock.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 del _active[_get_ident()]
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 _active_limbo_lock.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
406
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 def join(self, timeout=None):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 assert self.__initialized, "Thread.__init__() not called"
8474
049aa3a8a172 Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents: 8454
diff changeset
409 assert self.__started, "cannot join thread before it is started"
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 assert self is not currentThread(), "cannot join current thread"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 if not self.__stopped:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 self._note("%s.join(): waiting until thread stops", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 self.__block.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 if timeout is None:
8474
049aa3a8a172 Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents: 8454
diff changeset
416 while not self.__stopped:
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 self.__block.wait()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 self._note("%s.join(): thread stopped", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 else:
8454
2bd8d520ffac Two places where _time() should be used said time.time(), which
Guido van Rossum <guido@python.org>
parents: 8394
diff changeset
421 deadline = _time() + timeout
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 while not self.__stopped:
8454
2bd8d520ffac Two places where _time() should be used said time.time(), which
Guido van Rossum <guido@python.org>
parents: 8394
diff changeset
423 delay = deadline - _time()
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 if delay <= 0:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 self._note("%s.join(): timed out", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 break
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 self.__block.wait(delay)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 self._note("%s.join(): thread stopped", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 self.__block.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 def getName(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 assert self.__initialized, "Thread.__init__() not called"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 return self.__name
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 def setName(self, name):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 assert self.__initialized, "Thread.__init__() not called"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 self.__name = str(name)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
441
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 def isAlive(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 assert self.__initialized, "Thread.__init__() not called"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 return self.__started and not self.__stopped
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15586
diff changeset
445
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 def isDaemon(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 assert self.__initialized, "Thread.__init__() not called"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 return self.__daemonic
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 def setDaemon(self, daemonic):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 assert self.__initialized, "Thread.__init__() not called"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 assert not self.__started, "cannot set daemon status of active thread"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 self.__daemonic = daemonic
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
455
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 # Special thread class to represent the main thread
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 # This is garbage collected through an exit handler
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
458
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 class _MainThread(Thread):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
460
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 def __init__(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 Thread.__init__(self, name="MainThread")
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 self._Thread__started = 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 _active_limbo_lock.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 _active[_get_ident()] = self
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 _active_limbo_lock.release()
14063
17b7fc440195 Revise to use atexit instead of monkeying with sys.exitfunc directly.
Fred Drake <fdrake@acm.org>
parents: 12813
diff changeset
467 import atexit
17b7fc440195 Revise to use atexit instead of monkeying with sys.exitfunc directly.
Fred Drake <fdrake@acm.org>
parents: 12813
diff changeset
468 atexit.register(self.__exitfunc)
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 def _set_daemon(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 return 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
472
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 def __exitfunc(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 self._Thread__stop()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 t = _pickSomeNonDaemonThread()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 if t:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 self._note("%s: waiting for other threads", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 while t:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 t.join()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 t = _pickSomeNonDaemonThread()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 self._note("%s: exiting", self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 self._Thread__delete()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
485
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 def _pickSomeNonDaemonThread():
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 for t in enumerate():
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 if not t.isDaemon() and t.isAlive():
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 return t
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 return None
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
491
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 # Dummy thread class to represent threads not started here.
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 # These aren't garbage collected when they die,
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 # nor can they be waited for.
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 # Their purpose is to return *something* from currentThread().
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 # They are marked as daemon threads so we won't wait for them
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 # when we exit (conform previous semantics).
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
499
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 class _DummyThread(Thread):
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15586
diff changeset
501
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 def __init__(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 Thread.__init__(self, name=_newname("Dummy-%d"))
11344
4c6c8effcb42 Duncan Grisby noted a typo in _DummyThread.
Guido van Rossum <guido@python.org>
parents: 8474
diff changeset
504 self._Thread__started = 1
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 _active_limbo_lock.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 _active[_get_ident()] = self
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 _active_limbo_lock.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
508
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 def _set_daemon(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 return 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
511
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 def join(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 assert 0, "cannot join a dummy thread"
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
514
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
515
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 # Global API functions
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
517
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 def currentThread():
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 return _active[_get_ident()]
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 except KeyError:
15586
ff84112dabc8 Comment out a debugging print statement that triggered a complaint in
Guido van Rossum <guido@python.org>
parents: 14063
diff changeset
522 ##print "currentThread(): no current thread for", _get_ident()
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 return _DummyThread()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
524
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 def activeCount():
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 _active_limbo_lock.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 count = len(_active) + len(_limbo)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 _active_limbo_lock.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 return count
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
530
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 def enumerate():
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 _active_limbo_lock.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 active = _active.values() + _limbo.values()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 _active_limbo_lock.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 return active
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
536
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
537
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 # Create the main thread object
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
539
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 _MainThread()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
541
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
542
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 # Self-test code
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
544
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 def _test():
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
546
8394
93aa08c77e60 Use random instead of whrandom.
Guido van Rossum <guido@python.org>
parents: 7949
diff changeset
547 import random
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
548
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 class BoundedQueue(_Verbose):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
550
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 def __init__(self, limit):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 _Verbose.__init__(self)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 self.mon = RLock()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 self.rc = Condition(self.mon)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 self.wc = Condition(self.mon)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 self.limit = limit
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 self.queue = []
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
558
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 def put(self, item):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 self.mon.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 while len(self.queue) >= self.limit:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 self._note("put(%s): queue full", item)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 self.wc.wait()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 self.queue.append(item)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 self._note("put(%s): appended, length now %d",
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 item, len(self.queue))
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 self.rc.notify()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 self.mon.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
569
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 def get(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 self.mon.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 while not self.queue:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 self._note("get(): queue empty")
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 self.rc.wait()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 item = self.queue[0]
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 del self.queue[0]
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 self._note("get(): got %s, %d left", item, len(self.queue))
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 self.wc.notify()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 self.mon.release()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 return item
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
581
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 class ProducerThread(Thread):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
583
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 def __init__(self, queue, quota):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 Thread.__init__(self, name="Producer")
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 self.queue = queue
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 self.quota = quota
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
588
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 def run(self):
8394
93aa08c77e60 Use random instead of whrandom.
Guido van Rossum <guido@python.org>
parents: 7949
diff changeset
590 from random import random
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 counter = 0
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 while counter < self.quota:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 counter = counter + 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 self.queue.put("%s.%d" % (self.getName(), counter))
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 _sleep(random() * 0.00001)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
596
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
597
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 class ConsumerThread(Thread):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
599
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 def __init__(self, queue, count):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 Thread.__init__(self, name="Consumer")
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 self.queue = queue
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 self.count = count
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
604
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 def run(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 while self.count > 0:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 item = self.queue.get()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 print item
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 self.count = self.count - 1
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
610
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 import time
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
612
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 NP = 3
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 QL = 4
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 NI = 5
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
616
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 Q = BoundedQueue(QL)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 P = []
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 for i in range(NP):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 t = ProducerThread(Q, NI)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 t.setName("Producer-%d" % (i+1))
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 P.append(t)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 C = ConsumerThread(Q, NI*NP)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
624 for t in P:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
625 t.start()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 _sleep(0.000001)
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
627 C.start()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
628 for t in P:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 t.join()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 C.join()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
631
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 if __name__ == '__main__':
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 _test()