annotate Lib/threading.py @ 19283:b865cd5c1925 legacy-trunk

Added comments before recently added/assigned slots in the type object, so the backward compatibility issues will be easier to understand. I only added comments indicating additions and assignments back to Python 2.0.
author Fred Drake <fdrake@acm.org>
date Wed, 15 Aug 2001 18:32:33 +0000
parents b484b207e964
children 6386f01f2401
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:
19184
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
194 # Balancing act: We can't afford a pure busy loop, so we
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
195 # have to sleep; but if we sleep the whole timeout time,
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
196 # we'll be unresponsive. The scheme here sleeps very
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
197 # little at first, longer as time goes on, but never longer
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
198 # than 20 times per second (or the timeout time remaining).
17303
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
199 endtime = _time() + timeout
19184
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
200 delay = 0.0005 # 500 us -> initial delay of 1 ms
17303
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
201 while 1:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
202 gotit = waiter.acquire(0)
19184
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
203 if gotit:
17303
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
204 break
19184
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
205 remaining = endtime - _time()
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
206 if remaining <= 0:
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
207 break
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
208 delay = min(delay * 2, remaining, .05)
17303
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
209 _sleep(delay)
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
210 if not gotit:
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): timed out", self, timeout)
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
213 try:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
214 self.__waiters.remove(waiter)
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
215 except ValueError:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
216 pass
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
217 else:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
218 if __debug__:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
219 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
220 finally:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
221 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
222
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 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
224 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
225 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
226 __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
227 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
228 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
229 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
230 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
231 return
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 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
233 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
234 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
235 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
236 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 __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
238 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
239 pass
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 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
242 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
243
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 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
246 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
247
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 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
249
11876
c27d3e8c45c4 Fix a typo in a comment
Andrew M. Kuchling <amk@amk.ca>
parents: 11707
diff changeset
250 # 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
251
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 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
253 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
254 _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
255 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
256 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
257
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 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
259 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
260 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
261 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
262 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
263 break
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 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
265 else:
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 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
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 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
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 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
272 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
273 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
274 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
275 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
276
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 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
279 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
280
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 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
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 # 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
284
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 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
286 _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
287 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
288 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
289
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 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
291 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
292
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 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
294 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
295 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
296 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
297 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
298
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 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
300 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
301 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
302 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
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 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
305 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
306 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
307 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
308 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
309
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 # 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
312 _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
313 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
314 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
315 _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
316 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
317
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 # 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
319 _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
320 _active = {}
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 _limbo = {}
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
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 # 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
325
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 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
327
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 __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
329
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 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
331 args=(), kwargs={}, verbose=None):
8474
049aa3a8a172 Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents: 8454
diff changeset
332 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
333 _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
334 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
335 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
336 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
337 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
338 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
339 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
340 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
341 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
342 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
343
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 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
345 # 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
346 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
347
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 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
349 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
350 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
351 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
352 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
353 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
354 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
355 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
356 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
357 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
358
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 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
360 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
361 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
362 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
363 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
364 _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
365 _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
366 _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
367 _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
368 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
369 _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
370
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 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
372 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
373 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
374
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 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
376 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 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
378 _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
379 _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
380 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
381 _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
382 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
383 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
384 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 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
386 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
387 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
388 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
389 except:
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(): 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
392 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
393 _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
394 _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
395 (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
396 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 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
398 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
399 finally:
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.__stop()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 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
402
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 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
404 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
405 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
406 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
407 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
408
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 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
410 _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
411 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
412 _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
413
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 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
415 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
416 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
417 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
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 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
420 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
421 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
422 if timeout is None:
8474
049aa3a8a172 Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents: 8454
diff changeset
423 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
424 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
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(): 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
427 else:
8454
2bd8d520ffac Two places where _time() should be used said time.time(), which
Guido van Rossum <guido@python.org>
parents: 8394
diff changeset
428 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
429 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
430 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
431 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
432 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
433 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
434 break
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 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
436 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 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
438 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
439 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
440
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 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
442 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
443 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
444
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 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
446 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
447 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
448
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 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
450 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
451 return self.__started and not self.__stopped
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15586
diff changeset
452
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 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
454 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
455 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
456
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 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
458 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
459 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
460 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
461
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 # 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
464 # 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
465
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 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
467
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 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
469 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
470 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
471 _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
472 _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
473 _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
474 import atexit
17b7fc440195 Revise to use atexit instead of monkeying with sys.exitfunc directly.
Fred Drake <fdrake@acm.org>
parents: 12813
diff changeset
475 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
476
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 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
478 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
479
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 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
481 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
482 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
483 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
484 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
485 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
486 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
487 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
488 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
489 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
490 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
491 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
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 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
494 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
495 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
496 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
497 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
498
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 # 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
501 # 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
502 # 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
503 # 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
504 # 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
505 # 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
506
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 class _DummyThread(Thread):
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15586
diff changeset
508
7949
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 __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
510 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
511 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
512 _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
513 _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
514 _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
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 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
517 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
518
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 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
520 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
521
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
522
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 # 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
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 currentThread():
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 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
528 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
529 ##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
530 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
531
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 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
533 _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
534 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
535 _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
536 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
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 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
539 _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
540 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
541 _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
542 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
543
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 # 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
546
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 _MainThread()
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
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 # 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
551
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 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
553
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 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
555
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 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
557 _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
558 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
559 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
560 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
561 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
562 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
563
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 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
565 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
566 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
567 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
568 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
569 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
570 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
571 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
572 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
573 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
574
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 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
576 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
577 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
578 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
579 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
580 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
581 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
582 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
583 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
584 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
585 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
586
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 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
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 __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
590 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
591 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
592 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
593
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 def run(self):
8394
93aa08c77e60 Use random instead of whrandom.
Guido van Rossum <guido@python.org>
parents: 7949
diff changeset
595 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
596 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
597 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
598 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
599 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
600 _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
601
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
602
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 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
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 __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
606 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
607 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
608 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
609
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 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
611 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
612 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
613 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
614 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
615
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 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
617 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
618 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
619
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 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
621 P = []
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 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
623 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
624 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
625 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
626 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
627 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
628 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
629 _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
630 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
631 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
632 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
633 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
634
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
635 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
636 _test()