Mercurial > cpython-lru_cache-weakref
annotate Lib/threading.py @ 13163:a09e8635534d legacy-trunk
Don't try to guess the name of a .def file -- if one is supplied, use it,
otherwise just generate an '/export:' option.
| author | Greg Ward <gward@python.net> |
|---|---|
| date | Thu, 29 Jun 2000 23:09:20 +0000 |
| parents | f1f9c30a4156 |
| children | 17b7fc440195 |
| 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): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 |
|
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() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 if timeout 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
|
189 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
|
190 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
|
191 self._note("%s.wait(): got it", self) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 endtime = _time() + timeout |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 delay = 0.000001 # 1 usec |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 while 1: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 gotit = waiter.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
|
197 if gotit or _time() >= endtime: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 break |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 _sleep(delay) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 if delay < 1.0: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 delay = delay * 2.0 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 if not gotit: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 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
|
204 self._note("%s.wait(%s): timed out", self, timeout) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 self.__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
|
207 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
|
208 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 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
|
211 self._note("%s.wait(%s): got it", self, timeout) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 self._acquire_restore(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
|
213 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 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
|
215 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
|
216 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
|
217 __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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 return |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 self._note("%s.notify(): 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
|
224 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
|
225 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
|
226 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
|
227 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 __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
|
229 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
|
230 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 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
|
233 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
|
234 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 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
|
237 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
|
238 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 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
|
240 |
| 11876 | 241 # 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
|
242 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 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
|
244 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
|
245 _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
|
246 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
|
247 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
|
248 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 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
|
253 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
|
254 break |
|
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.wait() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 self.__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
|
258 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
|
259 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
|
260 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
|
261 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 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
|
263 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
|
264 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
|
265 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
|
266 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
|
267 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 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
|
270 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
|
271 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 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
|
273 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 # 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
|
275 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 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
|
277 _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
|
278 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
|
279 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
|
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 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
|
282 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
|
283 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 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
|
285 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
|
286 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
|
287 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
|
288 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
|
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 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
|
291 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
|
292 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
|
293 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
|
294 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 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
|
296 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
|
297 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
|
298 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
|
299 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
|
300 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 # 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
|
303 _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
|
304 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
|
305 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
|
306 _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
|
307 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
|
308 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 # 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
|
310 _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
|
311 _active = {} |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 _limbo = {} |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 # 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
|
316 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 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
|
318 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 __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
|
320 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 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
|
322 args=(), kwargs={}, verbose=None): |
|
8474
049aa3a8a172
Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents:
8454
diff
changeset
|
323 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
|
324 _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
|
325 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
|
326 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
|
327 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
|
328 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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
335 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
|
336 # 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
|
337 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
|
338 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 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
|
340 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
|
341 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
|
342 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
|
343 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
|
344 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
|
345 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
|
346 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
|
347 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
|
348 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
|
349 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
350 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
|
351 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
|
352 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
|
353 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
|
354 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
|
355 _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
|
356 _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
|
357 _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
|
358 _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
|
359 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
|
360 _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
|
361 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 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
|
363 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
|
364 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
|
365 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 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
|
367 try: |
|
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 _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
|
370 _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
|
371 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
|
372 _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
|
373 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
|
374 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
|
375 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 self.run() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 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
|
378 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
|
379 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
|
380 except: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
381 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
|
382 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
|
383 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
|
384 _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
|
385 _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
|
386 (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
|
387 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
388 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
|
389 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
|
390 finally: |
|
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.__stop() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 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
|
393 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
394 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
|
395 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
|
396 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
|
397 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
|
398 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
|
399 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
400 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
|
401 _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
|
402 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
|
403 _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
|
404 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
405 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
|
406 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
|
407 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
|
408 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
|
409 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
|
410 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
|
411 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
|
412 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
|
413 if timeout is None: |
|
8474
049aa3a8a172
Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents:
8454
diff
changeset
|
414 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
|
415 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
|
416 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
|
417 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
|
418 else: |
|
8454
2bd8d520ffac
Two places where _time() should be used said time.time(), which
Guido van Rossum <guido@python.org>
parents:
8394
diff
changeset
|
419 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
|
420 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
|
421 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
|
422 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
|
423 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
|
424 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
|
425 break |
|
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.__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
|
427 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
428 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
|
429 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
|
430 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
|
431 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
432 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
|
433 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
|
434 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
|
435 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 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
|
437 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
|
438 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
|
439 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
440 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
|
441 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
|
442 return self.__started and 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
|
443 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
444 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
|
445 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
|
446 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
|
447 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
448 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
|
449 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
|
450 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
|
451 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
|
452 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
453 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
454 # 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
|
455 # 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
|
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 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
|
458 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
459 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
|
460 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
|
461 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
|
462 _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
|
463 _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
|
464 _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
|
465 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
466 self.__oldexitfunc = _sys.exitfunc |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
467 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
|
468 self.__oldexitfunc = None |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
469 _sys.exitfunc = self.__exitfunc |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
470 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
471 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
|
472 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
|
473 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
474 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
|
475 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
|
476 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
|
477 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
|
478 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
|
479 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
|
480 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
|
481 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
|
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 self.__oldexitfunc: |
|
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: calling exit handler", 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 self.__oldexitfunc() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
487 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
|
488 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
|
489 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
|
490 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
491 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
|
492 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
|
493 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
|
494 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
|
495 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
|
496 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
497 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
498 # 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
|
499 # 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
|
500 # 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
|
501 # 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
|
502 # 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
|
503 # 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
|
504 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
505 class _DummyThread(Thread): |
|
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 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
|
508 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
|
509 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
|
510 _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
|
511 _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
|
512 _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
|
513 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
514 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
|
515 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
|
516 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
517 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
|
518 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
|
519 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
520 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
521 # 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
|
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 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
|
524 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
525 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
|
526 except KeyError: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
527 print "currentThread(): no current thread for", _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 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
|
529 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
530 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
|
531 _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
|
532 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
|
533 _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
|
534 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
|
535 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
536 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
|
537 _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
|
538 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
|
539 _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
|
540 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
|
541 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
542 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
543 # 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
|
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 _MainThread() |
|
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 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
548 # 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
|
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 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
|
551 |
|
8394
93aa08c77e60
Use random instead of whrandom.
Guido van Rossum <guido@python.org>
parents:
7949
diff
changeset
|
552 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
|
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 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
|
617 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
618 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
|
619 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
|
620 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
|
621 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
622 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
|
623 P = [] |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
624 for 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
|
625 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
|
626 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
|
627 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
|
628 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
|
629 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
|
630 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
|
631 _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
|
632 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
|
633 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
|
634 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
|
635 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
|
636 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
637 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
|
638 _test() |
