annotate Lib/threading.py @ 32527:0da660e05e69 legacy-trunk

"#if WITH_THREAD" is incorrect; must be #ifdef instead; WITH_THREAD isn't always set to an integer value when it's defined.
author Tim Peters <tim.peters@gmail.com>
date Wed, 07 Jul 2004 20:42:07 +0000
parents 796ccb9082ef
children 97768d87394a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24833
ca58f5a4558b Docstring nits: The module is neither proposed nor new.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24830
diff changeset
1 """Thread 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
26905
7ea7637341d0 - prefer "import ... as" to "import / (assignments) / del" for most things
Fred Drake <fdrake@acm.org>
parents: 26883
diff changeset
3 import sys as _sys
7ea7637341d0 - prefer "import ... as" to "import / (assignments) / del" for most things
Fred Drake <fdrake@acm.org>
parents: 26883
diff changeset
4
7ea7637341d0 - prefer "import ... as" to "import / (assignments) / del" for most things
Fred Drake <fdrake@acm.org>
parents: 26883
diff changeset
5 try:
7ea7637341d0 - prefer "import ... as" to "import / (assignments) / del" for most things
Fred Drake <fdrake@acm.org>
parents: 26883
diff changeset
6 import thread
7ea7637341d0 - prefer "import ... as" to "import / (assignments) / del" for most things
Fred Drake <fdrake@acm.org>
parents: 26883
diff changeset
7 except ImportError:
7ea7637341d0 - prefer "import ... as" to "import / (assignments) / del" for most things
Fred Drake <fdrake@acm.org>
parents: 26883
diff changeset
8 del _sys.modules[__name__]
7ea7637341d0 - prefer "import ... as" to "import / (assignments) / del" for most things
Fred Drake <fdrake@acm.org>
parents: 26883
diff changeset
9 raise
7ea7637341d0 - prefer "import ... as" to "import / (assignments) / del" for most things
Fred Drake <fdrake@acm.org>
parents: 26883
diff changeset
10
7ea7637341d0 - prefer "import ... as" to "import / (assignments) / del" for most things
Fred Drake <fdrake@acm.org>
parents: 26883
diff changeset
11 from time import time as _time, sleep as _sleep
30862
d45f8981fdea Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 29725
diff changeset
12 from traceback import format_exc as _format_exc
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 30862
diff changeset
13 from collections import deque
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # Rename some stuff so "from threading import *" is safe
26883
2133cf355bfc Add __all__. (Brett Cannon.)
Guido van Rossum <guido@python.org>
parents: 26326
diff changeset
16 __all__ = ['activeCount', 'Condition', 'currentThread', 'enumerate', 'Event',
29676
052bfcbabb7b Provide dummy (do-nothing) settrace() and setprofile() functions until
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
17 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
052bfcbabb7b Provide dummy (do-nothing) settrace() and setprofile() functions until
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
18 'Timer', 'setprofile', 'settrace']
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 _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
21 _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
22 _get_ident = thread.get_ident
12813
f1f9c30a4156 patch from Charles Waldman--
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11876
diff changeset
23 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
24 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
25
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
29725
7d0af4561eed Make the classes exposed by threading.py new-style classes. This is
Tim Peters <tim.peters@gmail.com>
parents: 29724
diff changeset
27 # Debug support (adapted from ihooks.py).
7d0af4561eed Make the classes exposed by threading.py new-style classes. This is
Tim Peters <tim.peters@gmail.com>
parents: 29724
diff changeset
28 # All the major classes here derive from _Verbose. We force that to
7d0af4561eed Make the classes exposed by threading.py new-style classes. This is
Tim Peters <tim.peters@gmail.com>
parents: 29724
diff changeset
29 # be a new-style class so that all the major classes here are new-style.
7d0af4561eed Make the classes exposed by threading.py new-style classes. This is
Tim Peters <tim.peters@gmail.com>
parents: 29724
diff changeset
30 # This helps debugging (type(instance) is more revealing for instances
7d0af4561eed Make the classes exposed by threading.py new-style classes. This is
Tim Peters <tim.peters@gmail.com>
parents: 29724
diff changeset
31 # of new-style classes).
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
29724
3b7b4fd5f7ca Resolved minor XXX question in the obvious way.
Tim Peters <tim.peters@gmail.com>
parents: 29686
diff changeset
33 _VERBOSE = 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
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
29725
7d0af4561eed Make the classes exposed by threading.py new-style classes. This is
Tim Peters <tim.peters@gmail.com>
parents: 29724
diff changeset
37 class _Verbose(object):
7949
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"
29725
7d0af4561eed Make the classes exposed by threading.py new-style classes. This is
Tim Peters <tim.peters@gmail.com>
parents: 29724
diff changeset
53 class _Verbose(object):
7949
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
29679
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
59 # Support for profile and trace hooks
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
60
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
61 _profile_hook = None
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
62 _trace_hook = None
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
63
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
64 def setprofile(func):
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
65 global _profile_hook
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
66 _profile_hook = func
29686
371092ce5c2b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 29683
diff changeset
67
29679
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
68 def settrace(func):
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
69 global _trace_hook
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
70 _trace_hook = func
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 # 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
73
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 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
75
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 def RLock(*args, **kwargs):
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26905
diff changeset
77 return _RLock(*args, **kwargs)
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 class _RLock(_Verbose):
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15586
diff changeset
80
7949
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 __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
82 _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
83 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
84 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
85 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
86
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 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
88 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
89 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
90 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
91 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
92
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 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
108 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
109 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
110
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 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
112 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
113 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
114 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
115 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
116 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
117 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
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.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
120 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 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
122 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
123
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 # 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
125
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 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
127 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
128 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
129 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
130 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
131 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
132
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 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
134 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
135 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
136 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
137 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
138 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
139 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
140 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
141 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
142
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 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
144 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
145
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 def Condition(*args, **kwargs):
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26905
diff changeset
148 return _Condition(*args, **kwargs)
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 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
151
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 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
153 _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
154 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
155 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
156 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
157 # 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
158 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
159 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
160 # 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
161 # 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
162 # 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
163 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 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
165 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
166 pass
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 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
169 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
170 pass
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 try:
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._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
173 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
174 pass
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 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
176
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 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
178 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
179
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 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
181 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
182
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 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
184 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
185
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 def _is_owned(self):
24828
4cf502716d15 Explain a little more.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24827
diff changeset
187 # Return True if lock is owned by currentThread.
24827
e5a07cc81559 Explain a minor mystery.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24058
diff changeset
188 # This method is called only if __lock doesn't have _is_owned().
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 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
190 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
191 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
192 else:
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 21981
diff changeset
193 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
194
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 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
196 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
197 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
198 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
199 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
200 saved_state = self._release_save()
17303
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
201 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
202 if timeout is None:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
203 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
204 if __debug__:
17303
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
205 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
206 else:
19184
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
207 # 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
208 # 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
209 # 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
210 # 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
211 # 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
212 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
213 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
214 while True:
17303
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
215 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
216 if gotit:
17303
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
217 break
19184
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
218 remaining = endtime - _time()
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
219 if remaining <= 0:
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
220 break
2967ca96d07e _Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents: 17303
diff changeset
221 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
222 _sleep(delay)
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
223 if not gotit:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
224 if __debug__:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
225 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
226 try:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
227 self.__waiters.remove(waiter)
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
228 except ValueError:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
229 pass
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
230 else:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
231 if __debug__:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
232 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
233 finally:
836cdf8d1245 SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents: 15873
diff changeset
234 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
235
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 def 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
237 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
238 __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
239 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
240 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
241 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
242 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
243 return
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 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
245 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
246 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
247 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
248 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 __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
250 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
251 pass
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 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
254 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
255
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 def Semaphore(*args, **kwargs):
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26905
diff changeset
258 return _Semaphore(*args, **kwargs)
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 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
261
11876
c27d3e8c45c4 Fix a typo in a comment
Andrew M. Kuchling <amk@amk.ca>
parents: 11707
diff changeset
262 # 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
263
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 def __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
265 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
266 _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
267 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
268 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
269
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 def acquire(self, blocking=1):
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
271 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
272 self.__cond.acquire()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 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
274 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
275 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
276 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
277 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
278 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
279 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
280 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 self.__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
282 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
283 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
284 self, self.__value)
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
285 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
286 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
287 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
288
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 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
290 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
291 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
292 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
293 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
294 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
295 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
296 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
297
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298
19404
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
299 def BoundedSemaphore(*args, **kwargs):
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26905
diff changeset
300 return _BoundedSemaphore(*args, **kwargs)
19404
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
301
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
302 class _BoundedSemaphore(_Semaphore):
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
303 """Semaphore that checks that # releases is <= # acquires"""
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
304 def __init__(self, value=1, verbose=None):
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
305 _Semaphore.__init__(self, value, verbose)
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
306 self._initial_value = value
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
307
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
308 def release(self):
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
309 if self._Semaphore__value >= self._initial_value:
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
310 raise ValueError, "Semaphore released too many times"
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
311 return _Semaphore.release(self)
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
312
6125d9f92441 Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents: 19379
diff changeset
313
7949
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 Event(*args, **kwargs):
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26905
diff changeset
315 return _Event(*args, **kwargs)
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 class _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
318
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 # 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
320
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 def __init__(self, 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
322 _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
323 self.__cond = Condition(Lock())
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
324 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
325
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 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
327 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
328
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 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
330 self.__cond.acquire()
26326
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
331 try:
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
332 self.__flag = True
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
333 self.__cond.notifyAll()
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
334 finally:
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
335 self.__cond.release()
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
336
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 def 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
338 self.__cond.acquire()
26326
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
339 try:
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
340 self.__flag = False
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
341 finally:
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
342 self.__cond.release()
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
343
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 def 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
345 self.__cond.acquire()
26326
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
346 try:
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
347 if not self.__flag:
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
348 self.__cond.wait(timeout)
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
349 finally:
2b0e815c0b30 The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents: 24833
diff changeset
350 self.__cond.release()
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 # 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
353 _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
354 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
355 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
356 _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
357 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
358
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 # 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
360 _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
361 _active = {}
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 _limbo = {}
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
364
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 # 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
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 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
368
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
369 __initialized = False
32475
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
370 # Need to store a reference to sys.exc_info for printing
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
371 # out exceptions when a thread tries to use a global var. during interp.
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
372 # shutdown and thus raises an exception about trying to perform some
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
373 # operation on/with a NoneType
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
374 __exc_info = _sys.exc_info
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 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
377 args=(), kwargs={}, verbose=None):
8474
049aa3a8a172 Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents: 8454
diff changeset
378 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
379 _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
380 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
381 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
382 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
383 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
384 self.__daemonic = self._set_daemon()
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
385 self.__started = False
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
386 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
387 self.__block = Condition(Lock())
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
388 self.__initialized = True
32475
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
389 # sys.stderr is not stored in the class like
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
390 # sys.exc_info since it can be changed between instances
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
391 self.__stderr = _sys.stderr
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
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 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
394 # 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
395 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
396
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 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
398 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
399 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
400 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
401 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
402 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
403 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
404 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
405 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
406 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
407
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 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
409 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
410 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
411 if __debug__:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 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
413 _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
414 _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
415 _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
416 _start_new_thread(self.__bootstrap, ())
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
417 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
418 _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
419
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 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
421 if self.__target:
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26905
diff changeset
422 self.__target(*self.__args, **self.__kwargs)
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 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
425 try:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
426 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
427 _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
428 _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
429 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
430 _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
431 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
432 self._note("%s.__bootstrap(): thread started", self)
29679
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
433
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
434 if _trace_hook:
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
435 self._note("%s.__bootstrap(): registering trace hook", self)
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
436 _sys.settrace(_trace_hook)
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
437 if _profile_hook:
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
438 self._note("%s.__bootstrap(): registering profile hook", self)
d0f15977f37f Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29676
diff changeset
439 _sys.setprofile(_profile_hook)
29686
371092ce5c2b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 29683
diff changeset
440
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 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
443 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
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 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
446 except:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 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
448 self._note("%s.__bootstrap(): unhandled exception", self)
32475
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
449 # If sys.stderr is no more (most likely from interpreter
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
450 # shutdown) use self.__stderr. Otherwise still use sys (as in
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
451 # _sys) in case sys.stderr was redefined since the creation of
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
452 # self.
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
453 if _sys:
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
454 _sys.stderr.write("Exception in thread %s:\n%s\n" %
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
455 (self.getName(), _format_exc()))
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
456 else:
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
457 # Do the best job possible w/o a huge amt. of code to
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
458 # approximate a traceback (code ideas from
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
459 # Lib/traceback.py)
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
460 exc_type, exc_value, exc_tb = self.__exc_info()
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
461 try:
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
462 print>>self.__stderr, (
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
463 "Exception in thread " + self.getName() +
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
464 " (most likely raised during interpreter shutdown):")
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
465 print>>self.__stderr, (
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
466 "Traceback (most recent call last):")
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
467 while exc_tb:
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
468 print>>self.__stderr, (
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
469 ' File "%s", line %s, in %s' %
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
470 (exc_tb.tb_frame.f_code.co_filename,
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
471 exc_tb.tb_lineno,
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
472 exc_tb.tb_frame.f_code.co_name))
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
473 exc_tb = exc_tb.tb_next
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
474 print>>self.__stderr, ("%s: %s" % (exc_type, exc_value))
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
475 # Make sure that exc_tb gets deleted since it is a memory
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
476 # hog; deleting everything else is just for thoroughness
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
477 finally:
796ccb9082ef threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents: 31629
diff changeset
478 del exc_type, exc_value, exc_tb
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 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 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
481 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
482 finally:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 self.__stop()
21589
d04eb8d664e2 Thread.__bootstrap(): ignore exceptions in the self.__delete() call in
Guido van Rossum <guido@python.org>
parents: 19953
diff changeset
484 try:
d04eb8d664e2 Thread.__bootstrap(): ignore exceptions in the self.__delete() call in
Guido van Rossum <guido@python.org>
parents: 19953
diff changeset
485 self.__delete()
d04eb8d664e2 Thread.__bootstrap(): ignore exceptions in the self.__delete() call in
Guido van Rossum <guido@python.org>
parents: 19953
diff changeset
486 except:
d04eb8d664e2 Thread.__bootstrap(): ignore exceptions in the self.__delete() call in
Guido van Rossum <guido@python.org>
parents: 19953
diff changeset
487 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
488
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 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
490 self.__block.acquire()
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
491 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
492 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
493 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
494
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 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
496 _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
497 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
498 _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
499
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 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
501 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
502 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
503 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
504 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
505 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
506 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
507 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
508 if timeout is None:
8474
049aa3a8a172 Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents: 8454
diff changeset
509 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
510 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
511 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
512 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
513 else:
8454
2bd8d520ffac Two places where _time() should be used said time.time(), which
Guido van Rossum <guido@python.org>
parents: 8394
diff changeset
514 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
515 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
516 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
517 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
518 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
519 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
520 break
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 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
522 else:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 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
524 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
525 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
526
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 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
528 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
529 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
530
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 def 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
532 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
533 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
534
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 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
536 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
537 return self.__started and not self.__stopped
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15586
diff changeset
538
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 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
540 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
541 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
542
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 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
544 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
545 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
546 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
547
19727
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
548 # 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
549
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
550 def Timer(*args, **kwargs):
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
551 return _Timer(*args, **kwargs)
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
552
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
553 class _Timer(Thread):
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
554 """Call a function after a specified number of seconds:
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19727
diff changeset
555
19727
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
556 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
557 t.start()
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
558 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
559 """
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19727
diff changeset
560
19727
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
561 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
562 Thread.__init__(self)
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
563 self.interval = interval
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
564 self.function = function
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
565 self.args = args
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
566 self.kwargs = kwargs
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
567 self.finished = Event()
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19727
diff changeset
568
19727
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
569 def cancel(self):
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
570 """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
571 self.finished.set()
19953
0f9df563b65d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19727
diff changeset
572
19727
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
573 def run(self):
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
574 self.finished.wait(self.interval)
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
575 if not self.finished.isSet():
f3036d4e3c22 Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents: 19404
diff changeset
576 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
577 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
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 # 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
580 # 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
581
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 class _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
583
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 def __init__(self):
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 Thread.__init__(self, name="MainThread")
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
586 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
587 _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
588 _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
589 _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
590 import atexit
17b7fc440195 Revise to use atexit instead of monkeying with sys.exitfunc directly.
Fred Drake <fdrake@acm.org>
parents: 12813
diff changeset
591 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
592
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 def _set_daemon(self):
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
594 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
595
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 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
597 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
598 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
599 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
600 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
601 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
602 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
603 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
604 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
605 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
606 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
607 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
608
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 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
610 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
611 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
612 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
613 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
614
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
615
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 # 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
617 # 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
618 # 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
619 # 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
620 # 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
621 # 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
622
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 class _DummyThread(Thread):
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15586
diff changeset
624
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
625 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
626 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
627 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
628 _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
629 _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
630 _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
631
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 def _set_daemon(self):
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
633 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
634
21981
4e9cc26e03fd SF #515023. Make _DummyThread.join() signature match base class (Thread)
Neal Norwitz <nnorwitz@gmail.com>
parents: 21589
diff changeset
635 def join(self, timeout=None):
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
636 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
637
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
638
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 # 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
640
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 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
642 try:
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
643 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
644 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
645 ##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
646 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
647
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
648 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
649 _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
650 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
651 _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
652 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
653
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 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
655 _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
656 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
657 _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
658 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
659
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 # 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
661
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 _MainThread()
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
663
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
664
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 # 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
666
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 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
668
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
669 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
670
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 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
672 _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
673 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
674 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
675 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
676 self.limit = limit
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 30862
diff changeset
677 self.queue = deque()
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
678
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 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
680 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
681 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
682 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
683 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
684 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
685 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
686 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
687 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
688 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
689
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 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
691 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
692 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
693 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
694 self.rc.wait()
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents: 30862
diff changeset
695 item = self.queue.popleft()
7949
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 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
697 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
698 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
699 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
700
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
701 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
702
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 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
704 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
705 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
706 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
707
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 def run(self):
8394
93aa08c77e60 Use random instead of whrandom.
Guido van Rossum <guido@python.org>
parents: 7949
diff changeset
709 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
710 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
711 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
712 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
713 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
714 _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
715
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
716
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 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
718
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 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
720 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
721 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
722 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
723
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 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
725 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
726 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
727 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
728 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
729
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 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
731 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
732 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
733
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
734 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
735 P = []
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 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
737 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
738 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
739 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
740 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
741 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
742 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
743 _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
744 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
745 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
746 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
747 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
748
58d6815af002 New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
749 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
750 _test()