Mercurial > cpython-lru_cache-weakref
annotate Lib/threading.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 0eb56a8e6262 |
| children | 2cdec848b74c |
| 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 |
|
46220
600f2ff8aa9e
Block the sys.exc_clear -3 warning from threading.py.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
46182
diff
changeset
|
11 import warnings |
|
47601
8dc4312a2f75
add aliases to threading module
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
12 |
|
8dc4312a2f75
add aliases to threading module
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
13 from functools import wraps |
|
26905
7ea7637341d0
- prefer "import ... as" to "import / (assignments) / del" for most things
Fred Drake <fdrake@acm.org>
parents:
26883
diff
changeset
|
14 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
|
15 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
|
16 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
|
17 |
|
48942
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
18 # Note regarding PEP 8 compliant aliases |
|
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
19 # This threading model was originally inspired by Java, and inherited |
|
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
20 # the convention of camelCase function and method names from that |
|
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
21 # language. While those names are not in any imminent danger of being |
|
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
22 # deprecated, starting with Python 2.6, the module now provides a |
|
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
23 # PEP 8 compliant alias for any such method name. |
|
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
24 # Using the new PEP 8 compliant names also facilitates substitution |
|
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
25 # with the multiprocessing module, which doesn't provide the old |
|
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
26 # Java inspired names. |
|
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
27 |
|
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
28 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 # Rename some stuff so "from threading import *" is safe |
|
47606
696344ef8bc0
add old names back into __all__
Benjamin Peterson <benjamin@python.org>
parents:
47601
diff
changeset
|
30 __all__ = ['activeCount', 'active_count', 'Condition', 'currentThread', |
|
696344ef8bc0
add old names back into __all__
Benjamin Peterson <benjamin@python.org>
parents:
47601
diff
changeset
|
31 'current_thread', 'enumerate', 'Event', |
|
29676
052bfcbabb7b
Provide dummy (do-nothing) settrace() and setprofile() functions until
Tim Peters <tim.peters@gmail.com>
parents:
28089
diff
changeset
|
32 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', |
|
38794
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
33 'Timer', 'setprofile', 'settrace', 'local', 'stack_size'] |
|
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 _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
|
36 _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
|
37 _get_ident = thread.get_ident |
|
12813
f1f9c30a4156
patch from Charles Waldman--
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11876
diff
changeset
|
38 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
|
39 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
|
40 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
46220
600f2ff8aa9e
Block the sys.exc_clear -3 warning from threading.py.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
46182
diff
changeset
|
42 # sys.exc_clear is used to work around the fact that except blocks |
|
600f2ff8aa9e
Block the sys.exc_clear -3 warning from threading.py.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
46182
diff
changeset
|
43 # don't fully clear the exception until 3.0. |
|
600f2ff8aa9e
Block the sys.exc_clear -3 warning from threading.py.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
46182
diff
changeset
|
44 warnings.filterwarnings('ignore', category=DeprecationWarning, |
|
600f2ff8aa9e
Block the sys.exc_clear -3 warning from threading.py.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
46182
diff
changeset
|
45 module='threading', message='sys.exc_clear') |
|
600f2ff8aa9e
Block the sys.exc_clear -3 warning from threading.py.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
46182
diff
changeset
|
46 |
|
29725
7d0af4561eed
Make the classes exposed by threading.py new-style classes. This is
Tim Peters <tim.peters@gmail.com>
parents:
29724
diff
changeset
|
47 # 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
|
48 # 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
|
49 # 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
|
50 # 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
|
51 # 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
|
52 |
|
29724
3b7b4fd5f7ca
Resolved minor XXX question in the obvious way.
Tim Peters <tim.peters@gmail.com>
parents:
29686
diff
changeset
|
53 _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
|
54 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 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
|
56 |
|
29725
7d0af4561eed
Make the classes exposed by threading.py new-style classes. This is
Tim Peters <tim.peters@gmail.com>
parents:
29724
diff
changeset
|
57 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
|
58 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 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
|
60 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
|
61 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
|
62 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
|
63 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 def _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
|
65 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
|
66 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
|
67 format = "%s: %s\n" % ( |
|
48830
f970a4f7560f
fix a few get_name() calls and turn then to .name
Benjamin Peterson <benjamin@python.org>
parents:
48295
diff
changeset
|
68 current_thread().name, format) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 _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
|
70 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 # 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
|
73 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
|
74 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
|
75 pass |
|
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 _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
|
77 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 |
|
29679
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
79 # 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
|
80 |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
81 _profile_hook = None |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
82 _trace_hook = None |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
83 |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
84 def setprofile(func): |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
85 global _profile_hook |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
86 _profile_hook = func |
|
29686
371092ce5c2b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
29683
diff
changeset
|
87 |
|
29679
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
88 def settrace(func): |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
89 global _trace_hook |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
90 _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
|
91 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 # 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
|
93 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 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
|
95 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 def RLock(*args, **kwargs): |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26905
diff
changeset
|
97 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
|
98 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 class _RLock(_Verbose): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15586
diff
changeset
|
100 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 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
|
102 _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
|
103 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
|
104 self.__owner = None |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 self.__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
|
106 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 def __repr__(self): |
|
42410
b6068dd6ae00
Eliminate RLock race condition reported in SF bug #1764059
Nick Coghlan <ncoghlan@gmail.com>
parents:
41960
diff
changeset
|
108 owner = self.__owner |
|
57159
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57119
diff
changeset
|
109 try: |
|
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57119
diff
changeset
|
110 owner = _active[owner].name |
|
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57119
diff
changeset
|
111 except KeyError: |
|
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57119
diff
changeset
|
112 pass |
|
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57119
diff
changeset
|
113 return "<%s owner=%r count=%d>" % ( |
|
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57119
diff
changeset
|
114 self.__class__.__name__, owner, self.__count) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 def acquire(self, blocking=1): |
|
57159
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57119
diff
changeset
|
117 me = _get_ident() |
|
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57119
diff
changeset
|
118 if self.__owner == me: |
|
7949
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.__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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 if __debug__: |
|
34512
cb7896d77de5
Fixed typo in verbose output.
Brett Cannon <bcannon@gmail.com>
parents:
34363
diff
changeset
|
128 self._note("%s.acquire(%s): initial success", self, blocking) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 else: |
|
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(%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
|
132 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
|
133 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
134 __enter__ = acquire |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
135 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 def release(self): |
|
57159
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57119
diff
changeset
|
137 if self.__owner != _get_ident(): |
|
56830
97d7cd365dd5
Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52596
diff
changeset
|
138 raise RuntimeError("cannot release un-acquired lock") |
|
7949
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.__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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 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
|
147 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
|
148 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
149 def __exit__(self, t, v, tb): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
150 self.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
151 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 # 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
|
153 |
|
48131
5279d7961093
Remove a tuple unpacking in a parameter list to suppress the SyntaxWarning with
Brett Cannon <bcannon@gmail.com>
parents:
47985
diff
changeset
|
154 def _acquire_restore(self, count_owner): |
|
5279d7961093
Remove a tuple unpacking in a parameter list to suppress the SyntaxWarning with
Brett Cannon <bcannon@gmail.com>
parents:
47985
diff
changeset
|
155 count, owner = count_owner |
|
7949
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.__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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 def _is_owned(self): |
|
57159
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57119
diff
changeset
|
173 return self.__owner == _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
|
174 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 def Condition(*args, **kwargs): |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26905
diff
changeset
|
177 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
|
178 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 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
|
180 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 def __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
|
182 _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
|
183 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
|
184 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
|
185 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
|
186 # 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
|
187 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
|
188 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
|
189 # 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
|
190 # 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
|
191 # 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
|
192 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 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
|
194 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
|
195 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 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
|
198 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
|
199 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 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
|
202 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
|
203 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 self.__waiters = [] |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 |
|
38084
a812d0fae74a
Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents:
37983
diff
changeset
|
206 def __enter__(self): |
|
a812d0fae74a
Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents:
37983
diff
changeset
|
207 return self.__lock.__enter__() |
|
a812d0fae74a
Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents:
37983
diff
changeset
|
208 |
|
a812d0fae74a
Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents:
37983
diff
changeset
|
209 def __exit__(self, *args): |
|
a812d0fae74a
Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents:
37983
diff
changeset
|
210 return self.__lock.__exit__(*args) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
211 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 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
|
213 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
|
214 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 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
|
216 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
|
217 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 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
|
219 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
|
220 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 def _is_owned(self): |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
222 # Return True if lock is owned by current_thread. |
|
24827
e5a07cc81559
Explain a minor mystery.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24058
diff
changeset
|
223 # 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
|
224 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
|
225 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
|
226 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
|
227 else: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
21981
diff
changeset
|
228 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
|
229 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 def wait(self, timeout=None): |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
231 if not self._is_owned(): |
|
56830
97d7cd365dd5
Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52596
diff
changeset
|
232 raise RuntimeError("cannot wait on un-acquired lock") |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 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
|
234 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
|
235 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
|
236 saved_state = self._release_save() |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
237 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
|
238 if timeout is None: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
239 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
|
240 if __debug__: |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
241 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
|
242 else: |
|
19184
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
243 # 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
|
244 # 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
|
245 # 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
|
246 # 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
|
247 # 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
|
248 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
|
249 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
|
250 while True: |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
251 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
|
252 if gotit: |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
253 break |
|
19184
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
254 remaining = endtime - _time() |
|
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
255 if remaining <= 0: |
|
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
256 break |
|
2967ca96d07e
_Condition.wait(): never sleep longer than the timeout time remaining,
Tim Peters <tim.peters@gmail.com>
parents:
17303
diff
changeset
|
257 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
|
258 _sleep(delay) |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
259 if not gotit: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
260 if __debug__: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
261 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
|
262 try: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
263 self.__waiters.remove(waiter) |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
264 except ValueError: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
265 pass |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
266 else: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
267 if __debug__: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
268 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
|
269 finally: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
270 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
|
271 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 def notify(self, n=1): |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
273 if not self._is_owned(): |
|
56830
97d7cd365dd5
Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52596
diff
changeset
|
274 raise RuntimeError("cannot notify on un-acquired lock") |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 __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
|
276 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
|
277 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
|
278 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
|
279 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
|
280 return |
|
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._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
|
282 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
|
283 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
|
284 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
|
285 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 __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
|
287 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
|
288 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 |
|
48942
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
290 def notifyAll(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
|
291 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
|
292 |
|
48942
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
293 notify_all = notifyAll |
|
47601
8dc4312a2f75
add aliases to threading module
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
294 |
|
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 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 def Semaphore(*args, **kwargs): |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26905
diff
changeset
|
297 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
|
298 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 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
|
300 |
| 11876 | 301 # 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
|
302 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 def __init__(self, value=1, verbose=None): |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
304 if value < 0: |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
305 raise ValueError("semaphore initial value must be >= 0") |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 _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
|
307 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
|
308 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
|
309 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 def acquire(self, blocking=1): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
311 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
|
312 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
|
313 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
|
314 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
|
315 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
|
316 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
|
317 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
|
318 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
|
319 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
|
320 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 self.__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
|
322 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
|
323 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
|
324 self, self.__value) |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
325 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
|
326 self.__cond.release() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 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
|
328 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
329 __enter__ = acquire |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
330 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 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
|
338 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
|
339 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
340 def __exit__(self, t, v, tb): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
341 self.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
342 |
|
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 |
|
19404
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
344 def BoundedSemaphore(*args, **kwargs): |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26905
diff
changeset
|
345 return _BoundedSemaphore(*args, **kwargs) |
|
19404
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
346 |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
347 class _BoundedSemaphore(_Semaphore): |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
348 """Semaphore that checks that # releases is <= # acquires""" |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
349 def __init__(self, value=1, verbose=None): |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
350 _Semaphore.__init__(self, value, verbose) |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
351 self._initial_value = value |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
352 |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
353 def release(self): |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
354 if self._Semaphore__value >= self._initial_value: |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
355 raise ValueError, "Semaphore released too many times" |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
356 return _Semaphore.release(self) |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
357 |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
358 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 def Event(*args, **kwargs): |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26905
diff
changeset
|
360 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
|
361 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 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
|
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 # 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
|
365 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 def __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
|
367 _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
|
368 self.__cond = Condition(Lock()) |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
369 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
|
370 |
|
48942
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
371 def isSet(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
|
372 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
|
373 |
|
48942
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
374 is_set = isSet |
|
47601
8dc4312a2f75
add aliases to threading module
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
375 |
|
7949
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 set(self): |
|
44918
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
377 self.__cond.acquire() |
|
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
378 try: |
|
26326
2b0e815c0b30
The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents:
24833
diff
changeset
|
379 self.__flag = True |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
380 self.__cond.notify_all() |
|
44918
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
381 finally: |
|
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
382 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
|
383 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
384 def clear(self): |
|
44918
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
385 self.__cond.acquire() |
|
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
386 try: |
|
26326
2b0e815c0b30
The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents:
24833
diff
changeset
|
387 self.__flag = False |
|
44918
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
388 finally: |
|
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
389 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
|
390 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
391 def wait(self, timeout=None): |
|
44918
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
392 self.__cond.acquire() |
|
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
393 try: |
|
26326
2b0e815c0b30
The _Event class should be more careful with releasing its lock when
Guido van Rossum <guido@python.org>
parents:
24833
diff
changeset
|
394 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
|
395 self.__cond.wait(timeout) |
|
44918
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
396 finally: |
|
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
397 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
|
398 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
399 # 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
|
400 _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
|
401 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
|
402 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
|
403 _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
|
404 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
|
405 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
406 # 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
|
407 _active_limbo_lock = _allocate_lock() |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
408 _active = {} # maps thread id to Thread 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
|
409 _limbo = {} |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 # 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
|
413 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 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
|
415 |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
416 __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
|
417 # 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
|
418 # 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
|
419 # 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
|
420 # 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
|
421 __exc_info = _sys.exc_info |
|
46146
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45545
diff
changeset
|
422 # Keep sys.exc_clear too to clear the exception just before |
|
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45545
diff
changeset
|
423 # allowing .join() to return. |
|
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45545
diff
changeset
|
424 __exc_clear = _sys.exc_clear |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 def __init__(self, group=None, target=None, name=None, |
|
35236
d0c6965b525e
bug [ 1238170 ] threading.Thread uses {} as default argument
Georg Brandl <georg@python.org>
parents:
34512
diff
changeset
|
427 args=(), kwargs=None, verbose=None): |
|
8474
049aa3a8a172
Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents:
8454
diff
changeset
|
428 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
|
429 _Verbose.__init__(self, verbose) |
|
35236
d0c6965b525e
bug [ 1238170 ] threading.Thread uses {} as default argument
Georg Brandl <georg@python.org>
parents:
34512
diff
changeset
|
430 if kwargs is None: |
|
d0c6965b525e
bug [ 1238170 ] threading.Thread uses {} as default argument
Georg Brandl <georg@python.org>
parents:
34512
diff
changeset
|
431 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
|
432 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
|
433 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
|
434 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
|
435 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
|
436 self.__daemonic = self._set_daemon() |
|
47438
7eaaad09b6cd
Adds a Thread.getIdent() method to provide the _get_ident() value for
Gregory P. Smith <greg@mad-scientist.com>
parents:
46280
diff
changeset
|
437 self.__ident = None |
|
45545
3e435a211356
Thread.start() used sleep(0.000001) to make sure it didn't return before the
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
438 self.__started = Event() |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
439 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
|
440 self.__block = Condition(Lock()) |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
441 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
|
442 # 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
|
443 # 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
|
444 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
|
445 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
446 def _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
|
447 # Overridden in _MainThread and _DummyThread |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
448 return current_thread().daemon |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
449 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
450 def __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
|
451 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
|
452 status = "initial" |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
453 if self.__started.is_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
|
454 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
|
455 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
|
456 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
|
457 if self.__daemonic: |
|
47438
7eaaad09b6cd
Adds a Thread.getIdent() method to provide the _get_ident() value for
Gregory P. Smith <greg@mad-scientist.com>
parents:
46280
diff
changeset
|
458 status += " daemon" |
|
7eaaad09b6cd
Adds a Thread.getIdent() method to provide the _get_ident() value for
Gregory P. Smith <greg@mad-scientist.com>
parents:
46280
diff
changeset
|
459 if self.__ident is not None: |
|
7eaaad09b6cd
Adds a Thread.getIdent() method to provide the _get_ident() value for
Gregory P. Smith <greg@mad-scientist.com>
parents:
46280
diff
changeset
|
460 status += " %s" % self.__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
|
461 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
|
462 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
463 def start(self): |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
464 if not self.__initialized: |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
465 raise RuntimeError("thread.__init__() not called") |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
466 if self.__started.is_set(): |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
467 raise RuntimeError("thread already 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
|
468 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
|
469 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
|
470 _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
|
471 _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
|
472 _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
|
473 _start_new_thread(self.__bootstrap, ()) |
|
45545
3e435a211356
Thread.start() used sleep(0.000001) to make sure it didn't return before the
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
474 self.__started.wait() |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
475 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
476 def run(self): |
|
45478
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
477 try: |
|
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
478 if self.__target: |
|
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
479 self.__target(*self.__args, **self.__kwargs) |
|
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
480 finally: |
|
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
481 # Avoid a refcycle if the thread is running a function with |
|
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
482 # an argument that has a member that points to the thread. |
|
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
483 del 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
|
484 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
485 def __bootstrap(self): |
|
42677
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
486 # Wrapper around the real bootstrap code that ignores |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
487 # exceptions during interpreter cleanup. Those typically |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
488 # happen when a daemon thread wakes up at an unfortunate |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
489 # moment, finds the world around it destroyed, and raises some |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
490 # random exception *** while trying to report the exception in |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
491 # __bootstrap_inner() below ***. Those random exceptions |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
492 # don't help anybody, and they confuse users, so we suppress |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
493 # them. We suppress them only when it appears that the world |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
494 # indeed has already been destroyed, so that exceptions in |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
495 # __bootstrap_inner() during normal business hours are properly |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
496 # reported. Also, we only suppress them for daemonic threads; |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
497 # if a non-daemonic encounters this, something else is wrong. |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
498 try: |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
499 self.__bootstrap_inner() |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
500 except: |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
501 if self.__daemonic and _sys is None: |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
502 return |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
503 raise |
|
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
504 |
|
52596
21ba0b3fe819
Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49059
diff
changeset
|
505 def _set_ident(self): |
|
21ba0b3fe819
Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49059
diff
changeset
|
506 self.__ident = _get_ident() |
|
21ba0b3fe819
Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49059
diff
changeset
|
507 |
|
42677
0f50e091a36f
Add a hack (originally devised in a slightly different form by Thomas Wouters)
Guido van Rossum <guido@python.org>
parents:
42410
diff
changeset
|
508 def __bootstrap_inner(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
|
509 try: |
|
52596
21ba0b3fe819
Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49059
diff
changeset
|
510 self._set_ident() |
|
45545
3e435a211356
Thread.start() used sleep(0.000001) to make sure it didn't return before the
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
511 self.__started.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
|
512 _active_limbo_lock.acquire() |
|
47438
7eaaad09b6cd
Adds a Thread.getIdent() method to provide the _get_ident() value for
Gregory P. Smith <greg@mad-scientist.com>
parents:
46280
diff
changeset
|
513 _active[self.__ident] = 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
|
514 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
|
515 _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
|
516 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
|
517 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
|
518 |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
519 if _trace_hook: |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
520 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
|
521 _sys.settrace(_trace_hook) |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
522 if _profile_hook: |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
523 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
|
524 _sys.setprofile(_profile_hook) |
|
29686
371092ce5c2b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
29683
diff
changeset
|
525 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
526 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
527 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
|
528 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
|
529 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
|
530 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
|
531 except: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
532 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
|
533 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
|
534 # 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
|
535 # 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
|
536 # _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
|
537 # self. |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
538 if _sys: |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
539 _sys.stderr.write("Exception in thread %s:\n%s\n" % |
|
48830
f970a4f7560f
fix a few get_name() calls and turn then to .name
Benjamin Peterson <benjamin@python.org>
parents:
48295
diff
changeset
|
540 (self.name, _format_exc())) |
|
32475
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
541 else: |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
542 # 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
|
543 # 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
|
544 # 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
|
545 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
|
546 try: |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
547 print>>self.__stderr, ( |
|
48830
f970a4f7560f
fix a few get_name() calls and turn then to .name
Benjamin Peterson <benjamin@python.org>
parents:
48295
diff
changeset
|
548 "Exception in thread " + self.name + |
|
32475
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
549 " (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
|
550 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
|
551 "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
|
552 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
|
553 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
|
554 ' 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
|
555 (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
|
556 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
|
557 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
|
558 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
|
559 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
|
560 # 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
|
561 # 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
|
562 finally: |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
563 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
|
564 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
565 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
|
566 self._note("%s.__bootstrap(): normal return", self) |
|
46146
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45545
diff
changeset
|
567 finally: |
|
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45545
diff
changeset
|
568 # Prevent a race in |
|
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45545
diff
changeset
|
569 # test_threading.test_no_refcycle_through_target when |
|
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45545
diff
changeset
|
570 # the exception keeps the target alive past when we |
|
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45545
diff
changeset
|
571 # assert that it's dead. |
|
46182
fe53d1a36ef6
Revert my experiment. I found one reason of failures in test_logging.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46179
diff
changeset
|
572 self.__exc_clear() |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
573 finally: |
|
44870
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44869
diff
changeset
|
574 with _active_limbo_lock: |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44869
diff
changeset
|
575 self.__stop() |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44869
diff
changeset
|
576 try: |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44869
diff
changeset
|
577 # We don't call self.__delete() because it also |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44869
diff
changeset
|
578 # grabs _active_limbo_lock. |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44869
diff
changeset
|
579 del _active[_get_ident()] |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44869
diff
changeset
|
580 except: |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44869
diff
changeset
|
581 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
|
582 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
583 def __stop(self): |
|
44918
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
584 self.__block.acquire() |
|
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
585 self.__stopped = True |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
586 self.__block.notify_all() |
|
44918
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
587 self.__block.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
|
588 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
589 def __delete(self): |
|
32778
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
590 "Remove current thread from the dict of currently running threads." |
|
32774
8a91cd7d7e82
Fix bug where a KeyError was raised if -O was being used for the interpreter
Brett Cannon <bcannon@gmail.com>
parents:
32673
diff
changeset
|
591 |
|
32778
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
592 # Notes about running with dummy_thread: |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
593 # |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
594 # Must take care to not raise an exception if dummy_thread is being |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
595 # used (and thus this module is being used as an instance of |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
596 # dummy_threading). dummy_thread.get_ident() always returns -1 since |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
597 # there is only one thread if dummy_thread is being used. Thus |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
598 # len(_active) is always <= 1 here, and any Thread instance created |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
599 # overwrites the (if any) thread currently registered in _active. |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
600 # |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
601 # An instance of _MainThread is always created by 'threading'. This |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
602 # gets overwritten the instant an instance of Thread is created; both |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
603 # threads return -1 from dummy_thread.get_ident() and thus have the |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
604 # same key in the dict. So when the _MainThread instance created by |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
605 # 'threading' tries to clean itself up when atexit calls this method |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
606 # it gets a KeyError if another Thread instance was created. |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
607 # |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
608 # This all means that KeyError from trying to delete something from |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
609 # _active if dummy_threading is being used is a red herring. But |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
610 # since it isn't if dummy_threading is *not* being used then don't |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
611 # hide the exception. |
|
32774
8a91cd7d7e82
Fix bug where a KeyError was raised if -O was being used for the interpreter
Brett Cannon <bcannon@gmail.com>
parents:
32673
diff
changeset
|
612 |
|
44918
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
613 try: |
|
46280
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46220
diff
changeset
|
614 with _active_limbo_lock: |
|
32774
8a91cd7d7e82
Fix bug where a KeyError was raised if -O was being used for the interpreter
Brett Cannon <bcannon@gmail.com>
parents:
32673
diff
changeset
|
615 del _active[_get_ident()] |
|
46280
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46220
diff
changeset
|
616 # There must not be any python code between the previous line |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46220
diff
changeset
|
617 # and after the lock is released. Otherwise a tracing function |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46220
diff
changeset
|
618 # could try to acquire the lock again in the same thread, (in |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
619 # current_thread()), and would block. |
|
46280
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46220
diff
changeset
|
620 except KeyError: |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46220
diff
changeset
|
621 if 'dummy_threading' not in _sys.modules: |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46220
diff
changeset
|
622 raise |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
623 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
624 def join(self, timeout=None): |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
625 if not self.__initialized: |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
626 raise RuntimeError("Thread.__init__() not called") |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
627 if not self.__started.is_set(): |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
628 raise RuntimeError("cannot join thread before it is started") |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
629 if self is current_thread(): |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
630 raise RuntimeError("cannot join current thread") |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
631 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
632 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
633 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
|
634 self._note("%s.join(): waiting until thread stops", self) |
|
44918
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
635 self.__block.acquire() |
|
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
636 try: |
|
35932
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
637 if timeout is None: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
638 while not self.__stopped: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
639 self.__block.wait() |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
640 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
|
641 self._note("%s.join(): thread stopped", self) |
|
35932
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
642 else: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
643 deadline = _time() + timeout |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
644 while not self.__stopped: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
645 delay = deadline - _time() |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
646 if delay <= 0: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
647 if __debug__: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
648 self._note("%s.join(): timed out", self) |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
649 break |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
650 self.__block.wait(delay) |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
651 else: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
652 if __debug__: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
653 self._note("%s.join(): thread stopped", self) |
|
44918
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
654 finally: |
|
685e96d20881
Revert 60189 and restore performance.
Raymond Hettinger <python@rcn.com>
parents:
44870
diff
changeset
|
655 self.__block.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
|
656 |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
657 @property |
|
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
658 def name(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
|
659 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
|
660 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
|
661 |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
662 @name.setter |
|
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
663 def name(self, name): |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
664 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
|
665 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
|
666 |
|
48287
1d29e53ce2af
change threading.getIdent to a property
Benjamin Peterson <benjamin@python.org>
parents:
48131
diff
changeset
|
667 @property |
|
1d29e53ce2af
change threading.getIdent to a property
Benjamin Peterson <benjamin@python.org>
parents:
48131
diff
changeset
|
668 def ident(self): |
|
47438
7eaaad09b6cd
Adds a Thread.getIdent() method to provide the _get_ident() value for
Gregory P. Smith <greg@mad-scientist.com>
parents:
46280
diff
changeset
|
669 assert self.__initialized, "Thread.__init__() not called" |
|
7eaaad09b6cd
Adds a Thread.getIdent() method to provide the _get_ident() value for
Gregory P. Smith <greg@mad-scientist.com>
parents:
46280
diff
changeset
|
670 return self.__ident |
|
7eaaad09b6cd
Adds a Thread.getIdent() method to provide the _get_ident() value for
Gregory P. Smith <greg@mad-scientist.com>
parents:
46280
diff
changeset
|
671 |
|
48942
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
672 def isAlive(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
|
673 assert self.__initialized, "Thread.__init__() not called" |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
674 return self.__started.is_set() and not self.__stopped |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15586
diff
changeset
|
675 |
|
48942
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
676 is_alive = isAlive |
|
48295
5c105f9a78e8
add py3k warnings for old threading APIs
Benjamin Peterson <benjamin@python.org>
parents:
48290
diff
changeset
|
677 |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
678 @property |
|
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
679 def daemon(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
|
680 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
|
681 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
|
682 |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
683 @daemon.setter |
|
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
684 def daemon(self, daemonic): |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
685 if not self.__initialized: |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
686 raise RuntimeError("Thread.__init__() not called") |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
687 if self.__started.is_set(): |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
40542
diff
changeset
|
688 raise RuntimeError("cannot set daemon status of active 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
|
689 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
|
690 |
|
48290
9d1856aba9f2
bring back the old API
Benjamin Peterson <benjamin@python.org>
parents:
48288
diff
changeset
|
691 def isDaemon(self): |
|
9d1856aba9f2
bring back the old API
Benjamin Peterson <benjamin@python.org>
parents:
48288
diff
changeset
|
692 return self.daemon |
|
9d1856aba9f2
bring back the old API
Benjamin Peterson <benjamin@python.org>
parents:
48288
diff
changeset
|
693 |
|
9d1856aba9f2
bring back the old API
Benjamin Peterson <benjamin@python.org>
parents:
48288
diff
changeset
|
694 def setDaemon(self, daemonic): |
|
9d1856aba9f2
bring back the old API
Benjamin Peterson <benjamin@python.org>
parents:
48288
diff
changeset
|
695 self.daemon = daemonic |
|
9d1856aba9f2
bring back the old API
Benjamin Peterson <benjamin@python.org>
parents:
48288
diff
changeset
|
696 |
|
9d1856aba9f2
bring back the old API
Benjamin Peterson <benjamin@python.org>
parents:
48288
diff
changeset
|
697 def getName(self): |
|
9d1856aba9f2
bring back the old API
Benjamin Peterson <benjamin@python.org>
parents:
48288
diff
changeset
|
698 return self.name |
|
9d1856aba9f2
bring back the old API
Benjamin Peterson <benjamin@python.org>
parents:
48288
diff
changeset
|
699 |
|
9d1856aba9f2
bring back the old API
Benjamin Peterson <benjamin@python.org>
parents:
48288
diff
changeset
|
700 def setName(self, name): |
|
9d1856aba9f2
bring back the old API
Benjamin Peterson <benjamin@python.org>
parents:
48288
diff
changeset
|
701 self.name = name |
|
9d1856aba9f2
bring back the old API
Benjamin Peterson <benjamin@python.org>
parents:
48288
diff
changeset
|
702 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
703 # 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
|
704 |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
705 def Timer(*args, **kwargs): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
706 return _Timer(*args, **kwargs) |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
707 |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
708 class _Timer(Thread): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
709 """Call a function after a specified number of seconds: |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
710 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
711 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
|
712 t.start() |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
713 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
|
714 """ |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
715 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
716 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
|
717 Thread.__init__(self) |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
718 self.interval = interval |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
719 self.function = function |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
720 self.args = args |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
721 self.kwargs = kwargs |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
722 self.finished = Event() |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
723 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
724 def cancel(self): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
725 """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
|
726 self.finished.set() |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
727 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
728 def run(self): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
729 self.finished.wait(self.interval) |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
730 if not self.finished.is_set(): |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
731 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
|
732 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
|
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 # 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
|
735 # 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
|
736 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
737 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
|
738 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
739 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
|
740 Thread.__init__(self, name="MainThread") |
|
45545
3e435a211356
Thread.start() used sleep(0.000001) to make sure it didn't return before the
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
741 self._Thread__started.set() |
|
52596
21ba0b3fe819
Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49059
diff
changeset
|
742 self._set_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
|
743 _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
|
744 _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
|
745 _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
|
746 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
747 def _set_daemon(self): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
748 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
|
749 |
|
40542
4203f8e6e86a
Bug #1566280: Explicitly invoke threading._shutdown from Py_Main,
Martin v. Löwis <martin@v.loewis.de>
parents:
38794
diff
changeset
|
750 def _exitfunc(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
|
751 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
|
752 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
|
753 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
|
754 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
|
755 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
|
756 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
|
757 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
|
758 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
|
759 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
|
760 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
|
761 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
|
762 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
763 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
|
764 for t in enumerate(): |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
765 if not t.daemon and t.is_alive(): |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
766 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
|
767 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
|
768 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
769 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
770 # Dummy thread class to represent threads not started here. |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
771 # These aren't garbage collected when they die, nor can they be waited for. |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
772 # If they invoke anything in threading.py that calls current_thread(), they |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
773 # leave an entry in the _active dict forever after. |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
774 # Their purpose is to return *something* from current_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
|
775 # 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
|
776 # 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
|
777 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
778 class _DummyThread(Thread): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15586
diff
changeset
|
779 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
780 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
|
781 Thread.__init__(self, name=_newname("Dummy-%d")) |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
782 |
|
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
783 # Thread.__block consumes an OS-level locking primitive, which |
|
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
784 # can never be used by a _DummyThread. Since a _DummyThread |
|
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
785 # instance is immortal, that's bad, so release this resource. |
|
34361
8618168dca27
In _DummyThread objects the lock stored in __block (allocated thanks to
Brett Cannon <bcannon@gmail.com>
parents:
32778
diff
changeset
|
786 del self._Thread__block |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
787 |
|
45545
3e435a211356
Thread.start() used sleep(0.000001) to make sure it didn't return before the
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
788 self._Thread__started.set() |
|
52596
21ba0b3fe819
Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49059
diff
changeset
|
789 self._set_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
|
790 _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
|
791 _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
|
792 _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
|
793 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
794 def _set_daemon(self): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
795 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
|
796 |
|
21981
4e9cc26e03fd
SF #515023. Make _DummyThread.join() signature match base class (Thread)
Neal Norwitz <nnorwitz@gmail.com>
parents:
21589
diff
changeset
|
797 def join(self, timeout=None): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
798 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
|
799 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
800 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
801 # 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
|
802 |
|
48942
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
803 def currentThread(): |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
804 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
805 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
|
806 except KeyError: |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
807 ##print "current_thread(): 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
|
808 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
|
809 |
|
48942
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
810 current_thread = currentThread |
|
47601
8dc4312a2f75
add aliases to threading module
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
811 |
|
48942
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
812 def activeCount(): |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
813 _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
|
814 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
|
815 _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
|
816 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
|
817 |
|
48942
bc585b4d4606
remove py3k warnings about the threading api; update docs
Benjamin Peterson <benjamin@python.org>
parents:
48830
diff
changeset
|
818 active_count = activeCount |
|
47601
8dc4312a2f75
add aliases to threading module
Benjamin Peterson <benjamin@python.org>
parents:
47598
diff
changeset
|
819 |
|
57119
2ebad3dfcde3
Merged revisions 76117 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56830
diff
changeset
|
820 def _enumerate(): |
|
2ebad3dfcde3
Merged revisions 76117 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56830
diff
changeset
|
821 # Same as enumerate(), but without the lock. Internal use only. |
|
2ebad3dfcde3
Merged revisions 76117 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56830
diff
changeset
|
822 return _active.values() + _limbo.values() |
|
2ebad3dfcde3
Merged revisions 76117 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56830
diff
changeset
|
823 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
824 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
|
825 _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
|
826 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
|
827 _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
|
828 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
|
829 |
|
38794
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
830 from thread import stack_size |
|
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
831 |
|
40542
4203f8e6e86a
Bug #1566280: Explicitly invoke threading._shutdown from Py_Main,
Martin v. Löwis <martin@v.loewis.de>
parents:
38794
diff
changeset
|
832 # Create the main thread object, |
|
4203f8e6e86a
Bug #1566280: Explicitly invoke threading._shutdown from Py_Main,
Martin v. Löwis <martin@v.loewis.de>
parents:
38794
diff
changeset
|
833 # and make it available for the interpreter |
|
4203f8e6e86a
Bug #1566280: Explicitly invoke threading._shutdown from Py_Main,
Martin v. Löwis <martin@v.loewis.de>
parents:
38794
diff
changeset
|
834 # (Py_Main) as threading._shutdown. |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
835 |
|
40542
4203f8e6e86a
Bug #1566280: Explicitly invoke threading._shutdown from Py_Main,
Martin v. Löwis <martin@v.loewis.de>
parents:
38794
diff
changeset
|
836 _shutdown = _MainThread()._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
|
837 |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
838 # get thread-local implementation, either from the thread |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
839 # module, or from the python fallback |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
840 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
841 try: |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
842 from thread import _local as local |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
843 except ImportError: |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
844 from _threading_local import local |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
845 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
846 |
|
47985
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
847 def _after_fork(): |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
848 # This function is called by Python/ceval.c:PyEval_ReInitThreads which |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
849 # is called from PyOS_AfterFork. Here we cleanup threading module state |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
850 # that should not exist after a fork. |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
851 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
852 # Reset _active_limbo_lock, in case we forked while the lock was held |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
853 # by another (non-forked) thread. http://bugs.python.org/issue874900 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
854 global _active_limbo_lock |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
855 _active_limbo_lock = _allocate_lock() |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
856 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
857 # fork() only copied the current thread; clear references to others. |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
858 new_active = {} |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
859 current = current_thread() |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
860 with _active_limbo_lock: |
|
49059
fda689970b3f
Backport relevant part of r66274 (in issue #874900).
Antoine Pitrou <solipsis@pitrou.net>
parents:
48942
diff
changeset
|
861 for thread in _active.itervalues(): |
|
47985
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
862 if thread is current: |
|
49059
fda689970b3f
Backport relevant part of r66274 (in issue #874900).
Antoine Pitrou <solipsis@pitrou.net>
parents:
48942
diff
changeset
|
863 # There is only one active thread. We reset the ident to |
|
fda689970b3f
Backport relevant part of r66274 (in issue #874900).
Antoine Pitrou <solipsis@pitrou.net>
parents:
48942
diff
changeset
|
864 # its new value since it can have changed. |
|
fda689970b3f
Backport relevant part of r66274 (in issue #874900).
Antoine Pitrou <solipsis@pitrou.net>
parents:
48942
diff
changeset
|
865 ident = _get_ident() |
|
fda689970b3f
Backport relevant part of r66274 (in issue #874900).
Antoine Pitrou <solipsis@pitrou.net>
parents:
48942
diff
changeset
|
866 thread._Thread__ident = ident |
|
47985
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
867 new_active[ident] = thread |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
868 else: |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
869 # All the others are already stopped. |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
870 # We don't call _Thread__stop() because it tries to acquire |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
871 # thread._Thread__block which could also have been held while |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
872 # we forked. |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
873 thread._Thread__stopped = True |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
874 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
875 _limbo.clear() |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
876 _active.clear() |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
877 _active.update(new_active) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
878 assert len(_active) == 1 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
879 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47606
diff
changeset
|
880 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
881 # 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
|
882 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
883 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
|
884 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
885 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
|
886 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
887 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
|
888 _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
|
889 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
|
890 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
|
891 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
|
892 self.limit = limit |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
30862
diff
changeset
|
893 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
|
894 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
895 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
|
896 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
|
897 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
|
898 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
|
899 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
|
900 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
|
901 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
|
902 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
|
903 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
|
904 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
|
905 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
906 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
|
907 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
|
908 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
|
909 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
|
910 self.rc.wait() |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
30862
diff
changeset
|
911 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
|
912 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
|
913 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
|
914 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
|
915 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
|
916 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
917 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
|
918 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
919 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
|
920 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
|
921 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
|
922 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
|
923 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
924 def run(self): |
|
8394
93aa08c77e60
Use random instead of whrandom.
Guido van Rossum <guido@python.org>
parents:
7949
diff
changeset
|
925 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
|
926 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
|
927 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
|
928 counter = counter + 1 |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
929 self.queue.put("%s.%d" % (self.name, counter)) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
930 _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
|
931 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
932 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
933 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
|
934 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
935 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
|
936 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
|
937 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
|
938 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
|
939 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
940 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
|
941 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
|
942 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
|
943 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
|
944 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
|
945 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
946 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
|
947 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
|
948 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
|
949 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
950 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
|
951 P = [] |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
952 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
|
953 t = ProducerThread(Q, NI) |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
954 t.name = ("Producer-%d" % (i+1)) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
955 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
|
956 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
|
957 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
|
958 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
|
959 _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
|
960 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
|
961 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
|
962 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
|
963 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
|
964 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
965 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
|
966 _test() |
