Mercurial > cpython-lru_cache-weakref
annotate Lib/threading.py @ 24042:63fbbed10714 legacy-trunk
Simplify HTTPSConnection constructor.
See discussion in SF bug 458463.
| author | Jeremy Hylton <jeremy@alum.mit.edu> |
|---|---|
| date | Fri, 28 Jun 2002 23:38:14 +0000 |
| parents | e2f2187c3d2c |
| children | 37ffce2d9c3c |
| 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 |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
33 _VERBOSE = 0 # XXX Bool or int? |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 class _Verbose: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 def __init__(self, verbose=None): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 if verbose is None: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 verbose = _VERBOSE |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 self.__verbose = verbose |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 def _note(self, format, *args): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 if self.__verbose: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 format = format % args |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 format = "%s: %s\n" % ( |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 currentThread().getName(), format) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 _sys.stderr.write(format) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 # Disable this when using "python -O" |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 class _Verbose: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 def __init__(self, verbose=None): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 def _note(self, *args): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 # Synchronization classes |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 Lock = _allocate_lock |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 def RLock(*args, **kwargs): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 return apply(_RLock, args, kwargs) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 class _RLock(_Verbose): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15586
diff
changeset
|
68 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 def __init__(self, verbose=None): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 _Verbose.__init__(self, verbose) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 self.__block = _allocate_lock() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 self.__owner = None |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 self.__count = 0 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 def __repr__(self): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 return "<%s(%s, %d)>" % ( |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 self.__class__.__name__, |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 self.__owner and self.__owner.getName(), |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 self.__count) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 def acquire(self, blocking=1): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 me = currentThread() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 if self.__owner is me: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 self.__count = self.__count + 1 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 self._note("%s.acquire(%s): recursive success", self, blocking) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 return 1 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 rc = self.__block.acquire(blocking) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 if rc: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 self.__owner = me |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 self.__count = 1 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 self._note("%s.acquire(%s): initial succes", self, blocking) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 self._note("%s.acquire(%s): failure", self, blocking) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 return rc |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 def release(self): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 me = currentThread() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 assert self.__owner is me, "release() of un-acquire()d lock" |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 self.__count = count = self.__count - 1 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 if not count: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 self.__owner = None |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 self.__block.release() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 self._note("%s.release(): final release", self) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 self._note("%s.release(): non-final release", self) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 # Internal methods used by condition variables |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 def _acquire_restore(self, (count, owner)): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 self.__block.acquire() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 self.__count = count |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 self.__owner = owner |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 self._note("%s._acquire_restore()", self) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 def _release_save(self): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 self._note("%s._release_save()", self) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 count = self.__count |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 self.__count = 0 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 owner = self.__owner |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 self.__owner = None |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 self.__block.release() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 return (count, owner) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 def _is_owned(self): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 return self.__owner is currentThread() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 def Condition(*args, **kwargs): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 return apply(_Condition, args, kwargs) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 class _Condition(_Verbose): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 def __init__(self, lock=None, verbose=None): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 _Verbose.__init__(self, verbose) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 if lock is None: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 lock = RLock() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 self.__lock = lock |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 # Export the lock's acquire() and release() methods |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 self.acquire = lock.acquire |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 self.release = lock.release |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 # If the lock defines _release_save() and/or _acquire_restore(), |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 # these override the default implementations (which just call |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 # release() and acquire() on the lock). Ditto for _is_owned(). |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 self._release_save = lock._release_save |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 except AttributeError: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 self._acquire_restore = lock._acquire_restore |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 except AttributeError: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 self._is_owned = lock._is_owned |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 except AttributeError: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 self.__waiters = [] |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 def __repr__(self): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters)) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 def _release_save(self): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 self.__lock.release() # No state to save |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 def _acquire_restore(self, x): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 self.__lock.acquire() # Ignore saved state |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 def _is_owned(self): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 if self.__lock.acquire(0): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 self.__lock.release() |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
21981
diff
changeset
|
177 return False |
|
7949
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: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
21981
diff
changeset
|
179 return True |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 def wait(self, timeout=None): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 me = currentThread() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 assert self._is_owned(), "wait() of un-acquire()d lock" |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 waiter = _allocate_lock() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 waiter.acquire() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 self.__waiters.append(waiter) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 saved_state = self._release_save() |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
188 try: # restore state no matter what (e.g., KeyboardInterrupt) |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
189 if timeout is None: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
190 waiter.acquire() |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 if __debug__: |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
192 self._note("%s.wait(): got it", self) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 else: |
|
19184
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
194 # Balancing act: We can't afford a pure busy loop, so we |
|
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
195 # have to sleep; but if we sleep the whole timeout time, |
|
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
196 # we'll be unresponsive. The scheme here sleeps very |
|
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
197 # little at first, longer as time goes on, but never longer |
|
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
198 # than 20 times per second (or the timeout time remaining). |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
199 endtime = _time() + timeout |
|
19184
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
200 delay = 0.0005 # 500 us -> initial delay of 1 ms |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
201 while True: |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
202 gotit = waiter.acquire(0) |
|
19184
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
203 if gotit: |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
204 break |
|
19184
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
205 remaining = endtime - _time() |
|
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
206 if remaining <= 0: |
|
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
207 break |
|
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
208 delay = min(delay * 2, remaining, .05) |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
209 _sleep(delay) |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
210 if not gotit: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
211 if __debug__: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
212 self._note("%s.wait(%s): timed out", self, timeout) |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
213 try: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
214 self.__waiters.remove(waiter) |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
215 except ValueError: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
216 pass |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
217 else: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
218 if __debug__: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
219 self._note("%s.wait(%s): got it", self, timeout) |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
220 finally: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
221 self._acquire_restore(saved_state) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 def notify(self, n=1): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 me = currentThread() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 assert self._is_owned(), "notify() of un-acquire()d lock" |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 __waiters = self.__waiters |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 waiters = __waiters[:n] |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 if not waiters: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 self._note("%s.notify(): no waiters", self) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 return |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 self._note("%s.notify(): notifying %d waiter%s", self, n, |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 n!=1 and "s" or "") |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 for waiter in waiters: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 waiter.release() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 __waiters.remove(waiter) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 except ValueError: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 def notifyAll(self): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 self.notify(len(self.__waiters)) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 def Semaphore(*args, **kwargs): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 return apply(_Semaphore, args, kwargs) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 class _Semaphore(_Verbose): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 |
| 11876 | 250 # After Tim Peters' semaphore class, but not quite the same (no maximum) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 def __init__(self, value=1, verbose=None): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 assert value >= 0, "Semaphore initial value must be >= 0" |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 _Verbose.__init__(self, verbose) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 self.__cond = Condition(Lock()) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 self.__value = value |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 def acquire(self, blocking=1): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
259 rc = False |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 self.__cond.acquire() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 while self.__value == 0: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 if not blocking: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 break |
|
19377
6386f01f2401
add debug calls to self._note for the Semaphore class. This closes bug
Skip Montanaro <skip@pobox.com>
parents:
19210
diff
changeset
|
264 if __debug__: |
|
6386f01f2401
add debug calls to self._note for the Semaphore class. This closes bug
Skip Montanaro <skip@pobox.com>
parents:
19210
diff
changeset
|
265 self._note("%s.acquire(%s): blocked waiting, value=%s", |
|
6386f01f2401
add debug calls to self._note for the Semaphore class. This closes bug
Skip Montanaro <skip@pobox.com>
parents:
19210
diff
changeset
|
266 self, blocking, self.__value) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 self.__cond.wait() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 self.__value = self.__value - 1 |
|
19377
6386f01f2401
add debug calls to self._note for the Semaphore class. This closes bug
Skip Montanaro <skip@pobox.com>
parents:
19210
diff
changeset
|
270 if __debug__: |
|
19379
2a083e050617
of course I muffed it separating the notes code from the initial_value
Skip Montanaro <skip@pobox.com>
parents:
19377
diff
changeset
|
271 self._note("%s.acquire: success, value=%s", |
|
2a083e050617
of course I muffed it separating the notes code from the initial_value
Skip Montanaro <skip@pobox.com>
parents:
19377
diff
changeset
|
272 self, self.__value) |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
273 rc = True |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 self.__cond.release() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 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
|
276 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 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
|
278 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
|
279 self.__value = self.__value + 1 |
|
19377
6386f01f2401
add debug calls to self._note for the Semaphore class. This closes bug
Skip Montanaro <skip@pobox.com>
parents:
19210
diff
changeset
|
280 if __debug__: |
|
19379
2a083e050617
of course I muffed it separating the notes code from the initial_value
Skip Montanaro <skip@pobox.com>
parents:
19377
diff
changeset
|
281 self._note("%s.release: success, value=%s", |
|
2a083e050617
of course I muffed it separating the notes code from the initial_value
Skip Montanaro <skip@pobox.com>
parents:
19377
diff
changeset
|
282 self, self.__value) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 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
|
284 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
|
285 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 |
|
19404
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
287 def BoundedSemaphore(*args, **kwargs): |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
288 return apply(_BoundedSemaphore, args, kwargs) |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
289 |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
290 class _BoundedSemaphore(_Semaphore): |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
291 """Semaphore that checks that # releases is <= # acquires""" |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
292 def __init__(self, value=1, verbose=None): |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
293 _Semaphore.__init__(self, value, verbose) |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
294 self._initial_value = value |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
295 |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
296 def release(self): |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
297 if self._Semaphore__value >= self._initial_value: |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
298 raise ValueError, "Semaphore released too many times" |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
299 return _Semaphore.release(self) |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
300 |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
301 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 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
|
303 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
|
304 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 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
|
306 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 # 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
|
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 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
|
310 _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
|
311 self.__cond = Condition(Lock()) |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
312 self.__flag = False |
|
7949
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 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
|
315 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
|
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 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
|
318 self.__cond.acquire() |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
319 self.__flag = True |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 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
|
321 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
|
322 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 def 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
|
324 self.__cond.acquire() |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
325 self.__flag = False |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 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
|
327 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 # 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
|
335 _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
|
336 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
|
337 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
|
338 _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
|
339 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
|
340 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 # 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
|
342 _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
|
343 _active = {} |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 _limbo = {} |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
346 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 # 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
|
348 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 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
|
350 |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
351 __initialized = False |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 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
|
354 args=(), kwargs={}, verbose=None): |
|
8474
049aa3a8a172
Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents:
8454
diff
changeset
|
355 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
|
356 _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
|
357 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
|
358 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
|
359 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
|
360 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
|
361 self.__daemonic = self._set_daemon() |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
362 self.__started = False |
|
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
363 self.__stopped = False |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 self.__block = Condition(Lock()) |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
365 self.__initialized = True |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 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
|
368 # 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
|
369 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
|
370 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 def __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
|
372 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
|
373 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
|
374 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
|
375 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
|
376 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
|
377 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
|
378 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
|
379 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
|
380 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
|
381 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 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
|
383 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
|
384 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
|
385 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
|
386 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
|
387 _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
|
388 _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
|
389 _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
|
390 _start_new_thread(self.__bootstrap, ()) |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
391 self.__started = True |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 _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
|
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 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
|
395 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
|
396 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
|
397 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
398 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
|
399 try: |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
400 self.__started = True |
|
7949
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 _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
|
403 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
|
404 _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
|
405 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
|
406 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
|
407 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
408 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
|
409 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
|
410 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
|
411 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
|
412 except: |
|
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 __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 self._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
|
415 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
|
416 _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
|
417 _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
|
418 (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
|
419 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
420 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
|
421 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
|
422 finally: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 self.__stop() |
|
21589
d04eb8d664e2
Thread.__bootstrap(): ignore exceptions in the self.__delete() call in
Guido van Rossum <guido@python.org>
parents:
19953
diff
changeset
|
424 try: |
|
d04eb8d664e2
Thread.__bootstrap(): ignore exceptions in the self.__delete() call in
Guido van Rossum <guido@python.org>
parents:
19953
diff
changeset
|
425 self.__delete() |
|
d04eb8d664e2
Thread.__bootstrap(): ignore exceptions in the self.__delete() call in
Guido van Rossum <guido@python.org>
parents:
19953
diff
changeset
|
426 except: |
|
d04eb8d664e2
Thread.__bootstrap(): ignore exceptions in the self.__delete() call in
Guido van Rossum <guido@python.org>
parents:
19953
diff
changeset
|
427 pass |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
428 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
429 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
|
430 self.__block.acquire() |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
431 self.__stopped = True |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
432 self.__block.notifyAll() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
433 self.__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
|
434 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
435 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
|
436 _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
|
437 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
|
438 _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
|
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 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
|
441 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
|
442 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
|
443 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
|
444 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
|
445 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
|
446 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
|
447 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
|
448 if timeout is None: |
|
8474
049aa3a8a172
Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents:
8454
diff
changeset
|
449 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
|
450 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
|
451 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
|
452 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
|
453 else: |
|
8454
2bd8d520ffac
Two places where _time() should be used said time.time(), which
Guido van Rossum <guido@python.org>
parents:
8394
diff
changeset
|
454 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
|
455 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
|
456 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
|
457 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
|
458 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
|
459 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
|
460 break |
|
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.__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
|
462 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
463 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
|
464 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
|
465 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
|
466 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
467 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
|
468 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
|
469 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
|
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 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
|
472 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
|
473 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
|
474 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
475 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
|
476 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
|
477 return self.__started and not self.__stopped |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15586
diff
changeset
|
478 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
479 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
|
480 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
|
481 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
|
482 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
483 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
|
484 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
|
485 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
|
486 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
|
487 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
488 # The timer class was contributed by Itamar Shtull-Trauring |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
489 |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
490 def Timer(*args, **kwargs): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
491 return _Timer(*args, **kwargs) |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
492 |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
493 class _Timer(Thread): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
494 """Call a function after a specified number of seconds: |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
495 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
496 t = Timer(30.0, f, args=[], kwargs={}) |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
497 t.start() |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
498 t.cancel() # stop the timer's action if it's still waiting |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
499 """ |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
500 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
501 def __init__(self, interval, function, args=[], kwargs={}): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
502 Thread.__init__(self) |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
503 self.interval = interval |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
504 self.function = function |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
505 self.args = args |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
506 self.kwargs = kwargs |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
507 self.finished = Event() |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
508 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
509 def cancel(self): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
510 """Stop the timer if it hasn't finished yet""" |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
511 self.finished.set() |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
512 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
513 def run(self): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
514 self.finished.wait(self.interval) |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
515 if not self.finished.isSet(): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
516 self.function(*self.args, **self.kwargs) |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
517 self.finished.set() |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
518 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
519 # 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
|
520 # 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
|
521 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
522 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
|
523 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
524 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
|
525 Thread.__init__(self, name="MainThread") |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
526 self._Thread__started = True |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
527 _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
|
528 _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
|
529 _active_limbo_lock.release() |
|
14063
17b7fc440195
Revise to use atexit instead of monkeying with sys.exitfunc directly.
Fred Drake <fdrake@acm.org>
parents:
12813
diff
changeset
|
530 import atexit |
|
17b7fc440195
Revise to use atexit instead of monkeying with sys.exitfunc directly.
Fred Drake <fdrake@acm.org>
parents:
12813
diff
changeset
|
531 atexit.register(self.__exitfunc) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
532 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
533 def _set_daemon(self): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
534 return False |
|
7949
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 __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
|
537 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
|
538 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
|
539 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
|
540 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
|
541 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
|
542 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
|
543 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
|
544 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
|
545 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
|
546 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
|
547 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
|
548 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
549 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
|
550 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
|
551 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
|
552 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
|
553 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
|
554 |
|
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 # 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
|
557 # 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
|
558 # 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
|
559 # 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
|
560 # 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
|
561 # 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
|
562 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
563 class _DummyThread(Thread): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15586
diff
changeset
|
564 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
565 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
|
566 Thread.__init__(self, name=_newname("Dummy-%d")) |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
567 self._Thread__started = True |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
568 _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
|
569 _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
|
570 _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
|
571 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
572 def _set_daemon(self): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
573 return True |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
574 |
|
21981
4e9cc26e03fd
SF #515023. Make _DummyThread.join() signature match base class (Thread)
Neal Norwitz <nnorwitz@gmail.com>
parents:
21589
diff
changeset
|
575 def join(self, timeout=None): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
576 assert False, "cannot join a dummy thread" |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
577 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
578 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
579 # 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
|
580 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
581 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
|
582 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
583 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
|
584 except KeyError: |
|
15586
ff84112dabc8
Comment out a debugging print statement that triggered a complaint in
Guido van Rossum <guido@python.org>
parents:
14063
diff
changeset
|
585 ##print "currentThread(): no current thread for", _get_ident() |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
586 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
|
587 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
588 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
|
589 _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
|
590 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
|
591 _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
|
592 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
|
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 enumerate(): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
595 _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
|
596 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
|
597 _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
|
598 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
|
599 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
600 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
601 # 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
|
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 _MainThread() |
|
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 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
606 # 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
|
607 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
608 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
|
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 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
|
611 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
612 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
|
613 _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
|
614 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
|
615 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
|
616 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
|
617 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
|
618 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
|
619 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
620 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
|
621 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
|
622 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
|
623 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
|
624 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
|
625 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
|
626 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
|
627 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
|
628 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
|
629 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
|
630 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
631 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
|
632 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
|
633 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
|
634 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
|
635 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
|
636 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
|
637 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
|
638 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
|
639 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
|
640 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
|
641 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
|
642 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
643 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
|
644 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
645 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
|
646 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
|
647 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
|
648 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
|
649 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
650 def run(self): |
|
8394
93aa08c77e60
Use random instead of whrandom.
Guido van Rossum <guido@python.org>
parents:
7949
diff
changeset
|
651 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
|
652 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
|
653 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
|
654 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
|
655 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
|
656 _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
|
657 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
658 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
659 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
|
660 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
661 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
|
662 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
|
663 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
|
664 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
|
665 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
666 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
|
667 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
|
668 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
|
669 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
|
670 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
|
671 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
672 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
|
673 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
|
674 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
|
675 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
676 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
|
677 P = [] |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
678 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
|
679 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
|
680 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
|
681 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
|
682 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
|
683 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
|
684 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
|
685 _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
|
686 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
|
687 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
|
688 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
|
689 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
|
690 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
691 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
|
692 _test() |
