Mercurial > cpython-lru_cache-weakref
annotate Lib/threading.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 86a855ee0456 |
| children |
| 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 |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
4 import _thread |
|
26905
7ea7637341d0
- prefer "import ... as" to "import / (assignments) / del" for most things
Fred Drake <fdrake@acm.org>
parents:
26883
diff
changeset
|
5 |
|
76597
76d2e0761d18
Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents:
76422
diff
changeset
|
6 from time import sleep as _sleep |
|
76d2e0761d18
Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents:
76422
diff
changeset
|
7 try: |
|
76d2e0761d18
Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents:
76422
diff
changeset
|
8 from time import monotonic as _time |
|
76d2e0761d18
Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents:
76422
diff
changeset
|
9 except ImportError: |
|
76d2e0761d18
Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents:
76422
diff
changeset
|
10 from time import time as _time |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
29725
diff
changeset
|
11 from traceback import format_exc as _format_exc |
|
71361
2bedea96de60
Issue #12573: Add resource checks for dangling Thread and Process objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
12 from _weakrefset import WeakSet |
| 82861 | 13 from itertools import islice as _islice |
|
82592
0f86b51f8f8b
Issue #17385: Fix quadratic behavior in threading.Condition
Raymond Hettinger <python@rcn.com>
parents:
82591
diff
changeset
|
14 try: |
|
0f86b51f8f8b
Issue #17385: Fix quadratic behavior in threading.Condition
Raymond Hettinger <python@rcn.com>
parents:
82591
diff
changeset
|
15 from _collections import deque as _deque |
|
84442
7769c4d72806
Issue #18200: Back out usage of ModuleNotFoundError (8d28d44f3a9a)
Brett Cannon <brett@python.org>
parents:
84114
diff
changeset
|
16 except ImportError: |
|
82592
0f86b51f8f8b
Issue #17385: Fix quadratic behavior in threading.Condition
Raymond Hettinger <python@rcn.com>
parents:
82591
diff
changeset
|
17 from collections import deque as _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
|
18 |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
19 # Note regarding PEP 8 compliant names |
|
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
20 # This threading model was originally inspired by Java, and inherited |
|
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
21 # the convention of camelCase function and method names from that |
|
85221
e07f104133d5
#18705: fix a number of typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83497
diff
changeset
|
22 # language. Those original names are not in any imminent danger of |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
23 # being deprecated (even for Py3k),so this module provides them as an |
|
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
24 # alias for the PEP 8 compliant names |
|
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
25 # Note that using the new PEP 8 compliant names facilitates substitution |
|
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
26 # with the multiprocessing module, which doesn't provide the old |
|
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
27 # Java inspired names. |
|
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
28 |
|
67533
2244ac6c2a9b
Add entry for Barrier objects.
Raymond Hettinger <python@rcn.com>
parents:
67354
diff
changeset
|
29 __all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event', |
|
2244ac6c2a9b
Add entry for Barrier objects.
Raymond Hettinger <python@rcn.com>
parents:
67354
diff
changeset
|
30 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier', |
|
71701
bbeda42ea6a8
add ThreadError to threading.__all__ (closes #12679)
Benjamin Peterson <benjamin@python.org>
parents:
71560
diff
changeset
|
31 'Timer', 'ThreadError', 'setprofile', 'settrace', 'local', 'stack_size'] |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
32 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 # Rename some stuff so "from threading import *" is safe |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
34 _start_new_thread = _thread.start_new_thread |
|
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
35 _allocate_lock = _thread.allocate_lock |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
36 _set_sentinel = _thread._set_sentinel |
|
70530
5548197d1f22
Close #12028: Make threading._get_ident() public, rename it to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69710
diff
changeset
|
37 get_ident = _thread.get_ident |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
38 ThreadError = _thread.error |
|
57172
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
39 try: |
|
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
40 _CRLock = _thread.RLock |
|
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
41 except AttributeError: |
|
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
42 _CRLock = None |
|
60598
01d1fd775d16
Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents:
60377
diff
changeset
|
43 TIMEOUT_MAX = _thread.TIMEOUT_MAX |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
44 del _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
|
45 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 |
|
29679
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
47 # 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
|
48 |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
49 _profile_hook = None |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
50 _trace_hook = None |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
51 |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
52 def setprofile(func): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
53 """Set a profile function for all threads started from the threading module. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
54 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
55 The func will be passed to sys.setprofile() for each thread, before its |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
56 run() method is called. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
57 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
58 """ |
|
29679
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
59 global _profile_hook |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
60 _profile_hook = func |
|
29686
371092ce5c2b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
29683
diff
changeset
|
61 |
|
29679
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
62 def settrace(func): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
63 """Set a trace function for all threads started from the threading module. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
64 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
65 The func will be passed to sys.settrace() for each thread, before its run() |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
66 method is called. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
67 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
68 """ |
|
29679
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
69 global _trace_hook |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
70 _trace_hook = func |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 # Synchronization classes |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 Lock = _allocate_lock |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
76 def RLock(*args, **kwargs): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
77 """Factory function that returns a new reentrant lock. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
78 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
79 A reentrant lock must be released by the thread that acquired it. Once a |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
80 thread has acquired a reentrant lock, the same thread may acquire it again |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
81 without blocking; the thread must release it once for each time it has |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
82 acquired it. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
83 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
84 """ |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
85 if _CRLock is None: |
|
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
86 return _PyRLock(*args, **kwargs) |
|
57172
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
87 return _CRLock(*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
|
88 |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
89 class _RLock: |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
90 """This class implements reentrant lock objects. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
91 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
92 A reentrant lock must be released by the thread that acquired it. Once a |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
93 thread has acquired a reentrant lock, the same thread may acquire it |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
94 again without blocking; the thread must release it once for each time it |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
95 has acquired it. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
96 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
97 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15586
diff
changeset
|
98 |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
99 def __init__(self): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
100 self._block = _allocate_lock() |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
101 self._owner = None |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
102 self._count = 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
|
103 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 def __repr__(self): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
105 owner = self._owner |
|
57160
fe33562d1f95
Merged revisions 76138,76173 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57121
diff
changeset
|
106 try: |
|
fe33562d1f95
Merged revisions 76138,76173 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57121
diff
changeset
|
107 owner = _active[owner].name |
|
fe33562d1f95
Merged revisions 76138,76173 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57121
diff
changeset
|
108 except KeyError: |
|
fe33562d1f95
Merged revisions 76138,76173 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57121
diff
changeset
|
109 pass |
|
fe33562d1f95
Merged revisions 76138,76173 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57121
diff
changeset
|
110 return "<%s owner=%r count=%d>" % ( |
|
fe33562d1f95
Merged revisions 76138,76173 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57121
diff
changeset
|
111 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
|
112 |
|
60598
01d1fd775d16
Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents:
60377
diff
changeset
|
113 def acquire(self, blocking=True, timeout=-1): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
114 """Acquire a lock, blocking or non-blocking. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
115 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
116 When invoked without arguments: if this thread already owns the lock, |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
117 increment the recursion level by one, and return immediately. Otherwise, |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
118 if another thread owns the lock, block until the lock is unlocked. Once |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
119 the lock is unlocked (not owned by any thread), then grab ownership, set |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
120 the recursion level to one, and return. If more than one thread is |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
121 blocked waiting until the lock is unlocked, only one at a time will be |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
122 able to grab ownership of the lock. There is no return value in this |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
123 case. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
124 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
125 When invoked with the blocking argument set to true, do the same thing |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
126 as when called without arguments, and return true. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
127 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
128 When invoked with the blocking argument set to false, do not block. If a |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
129 call without an argument would block, return false immediately; |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
130 otherwise, do the same thing as when called without arguments, and |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
131 return true. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
132 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
133 When invoked with the floating-point timeout argument set to a positive |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
134 value, block for at most the number of seconds specified by timeout |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
135 and as long as the lock cannot be acquired. Return true if the lock has |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
136 been acquired, false if the timeout has elapsed. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
137 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
138 """ |
|
70530
5548197d1f22
Close #12028: Make threading._get_ident() public, rename it to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69710
diff
changeset
|
139 me = get_ident() |
|
57160
fe33562d1f95
Merged revisions 76138,76173 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57121
diff
changeset
|
140 if self._owner == me: |
|
82591
28b86525ef07
Update code to increment and decrement using the cleaner += 1 and -= 1 style.
Raymond Hettinger <python@rcn.com>
parents:
76597
diff
changeset
|
141 self._count += 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
|
142 return 1 |
|
60598
01d1fd775d16
Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents:
60377
diff
changeset
|
143 rc = self._block.acquire(blocking, timeout) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 if rc: |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
145 self._owner = me |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
146 self._count = 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
|
147 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
|
148 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
149 __enter__ = acquire |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
150 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 def release(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
152 """Release a lock, decrementing the recursion level. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
153 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
154 If after the decrement it is zero, reset the lock to unlocked (not owned |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
155 by any thread), and if any other threads are blocked waiting for the |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
156 lock to become unlocked, allow exactly one of them to proceed. If after |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
157 the decrement the recursion level is still nonzero, the lock remains |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
158 locked and owned by the calling thread. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
159 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
160 Only call this method when the calling thread owns the lock. A |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
161 RuntimeError is raised if this method is called when the lock is |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
162 unlocked. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
163 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
164 There is no return value. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
165 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
166 """ |
|
70530
5548197d1f22
Close #12028: Make threading._get_ident() public, rename it to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69710
diff
changeset
|
167 if self._owner != get_ident(): |
|
56894
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56002
diff
changeset
|
168 raise RuntimeError("cannot release un-acquired lock") |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
169 self._count = count = self._count - 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
|
170 if not count: |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
171 self._owner = None |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
172 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
|
173 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
174 def __exit__(self, t, v, tb): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
175 self.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
176 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 # 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
|
178 |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
179 def _acquire_restore(self, state): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
180 self._block.acquire() |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
181 self._count, self._owner = 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
|
182 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 def _release_save(self): |
|
69543
2c0da1c4f063
Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69457
diff
changeset
|
184 if self._count == 0: |
|
2c0da1c4f063
Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69457
diff
changeset
|
185 raise RuntimeError("cannot release un-acquired lock") |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
186 count = self._count |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
187 self._count = 0 |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
188 owner = self._owner |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
189 self._owner = None |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
190 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
|
191 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
|
192 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 def _is_owned(self): |
|
70530
5548197d1f22
Close #12028: Make threading._get_ident() public, rename it to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69710
diff
changeset
|
194 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
|
195 |
|
57172
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
196 _PyRLock = _RLock |
|
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
197 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
199 class Condition: |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
200 """Class that implements a condition variable. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
201 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
202 A condition variable allows one or more threads to wait until they are |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
203 notified by another thread. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
204 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
205 If the lock argument is given and not None, it must be a Lock or RLock |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
206 object, and it is used as the underlying lock. Otherwise, a new RLock object |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
207 is created and used as the underlying lock. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
208 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
209 """ |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
211 def __init__(self, lock=None): |
|
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 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
|
213 lock = RLock() |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
214 self._lock = 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
|
215 # 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
|
216 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
|
217 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
|
218 # 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
|
219 # 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
|
220 # 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
|
221 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 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
|
223 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
|
224 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 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
|
227 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
|
228 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 self._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
|
231 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
|
232 pass |
|
82592
0f86b51f8f8b
Issue #17385: Fix quadratic behavior in threading.Condition
Raymond Hettinger <python@rcn.com>
parents:
82591
diff
changeset
|
233 self._waiters = _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
|
234 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
235 def __enter__(self): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
236 return self._lock.__enter__() |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
237 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
238 def __exit__(self, *args): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
239 return self._lock.__exit__(*args) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
240 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 def __repr__(self): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
242 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters)) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 def _release_save(self): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
245 self._lock.release() # No state to save |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 def _acquire_restore(self, x): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
248 self._lock.acquire() # Ignore 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
|
249 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 def _is_owned(self): |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
251 # 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
|
252 # This method is called only if __lock doesn't have _is_owned(). |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
253 if self._lock.acquire(0): |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
254 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
|
255 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
|
256 else: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
21981
diff
changeset
|
257 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
|
258 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 def wait(self, timeout=None): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
260 """Wait until notified or until a timeout occurs. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
261 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
262 If the calling thread has not acquired the lock when this method is |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
263 called, a RuntimeError is raised. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
264 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
265 This method releases the underlying lock, and then blocks until it is |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
266 awakened by a notify() or notify_all() call for the same condition |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
267 variable in another thread, or until the optional timeout occurs. Once |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
268 awakened or timed out, it re-acquires the lock and returns. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
269 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
270 When the timeout argument is present and not None, it should be a |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
271 floating point number specifying a timeout for the operation in seconds |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
272 (or fractions thereof). |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
273 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
274 When the underlying lock is an RLock, it is not released using its |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
275 release() method, since this may not actually unlock the lock when it |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
276 was acquired multiple times recursively. Instead, an internal interface |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
277 of the RLock class is used, which really unlocks it even when it has |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
278 been recursively acquired several times. Another internal interface is |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
279 then used to restore the recursion level when the lock is reacquired. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
280 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
281 """ |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
282 if not self._is_owned(): |
|
56894
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56002
diff
changeset
|
283 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
|
284 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
|
285 waiter.acquire() |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
286 self._waiters.append(waiter) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 saved_state = self._release_save() |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
288 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
|
289 if timeout is None: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
290 waiter.acquire() |
|
65675
6dff581d30f3
#10218: return timeout status from Condition.wait, mirroring other primitives' behavior.
Georg Brandl <georg@python.org>
parents:
63166
diff
changeset
|
291 gotit = 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
|
292 else: |
|
60598
01d1fd775d16
Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents:
60377
diff
changeset
|
293 if timeout > 0: |
|
01d1fd775d16
Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents:
60377
diff
changeset
|
294 gotit = waiter.acquire(True, timeout) |
|
01d1fd775d16
Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents:
60377
diff
changeset
|
295 else: |
|
01d1fd775d16
Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents:
60377
diff
changeset
|
296 gotit = waiter.acquire(False) |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
297 if not gotit: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
298 try: |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
299 self._waiters.remove(waiter) |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
300 except ValueError: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
301 pass |
|
65675
6dff581d30f3
#10218: return timeout status from Condition.wait, mirroring other primitives' behavior.
Georg Brandl <georg@python.org>
parents:
63166
diff
changeset
|
302 return gotit |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
303 finally: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
304 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
|
305 |
|
66229
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
306 def wait_for(self, predicate, timeout=None): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
307 """Wait until a condition evaluates to True. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
308 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
309 predicate should be a callable which result will be interpreted as a |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
310 boolean value. A timeout may be provided giving the maximum time to |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
311 wait. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
312 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
313 """ |
|
66229
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
314 endtime = None |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
315 waittime = timeout |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
316 result = predicate() |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
317 while not result: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
318 if waittime is not None: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
319 if endtime is None: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
320 endtime = _time() + waittime |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
321 else: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
322 waittime = endtime - _time() |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
323 if waittime <= 0: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
324 break |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
325 self.wait(waittime) |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
326 result = predicate() |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
327 return result |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
328 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 def notify(self, n=1): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
330 """Wake up one or more threads waiting on this condition, if any. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
331 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
332 If the calling thread has not acquired the lock when this method is |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
333 called, a RuntimeError is raised. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
334 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
335 This method wakes up at most n of the threads waiting for the condition |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
336 variable; it is a no-op if no threads are waiting. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
337 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
338 """ |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
339 if not self._is_owned(): |
|
56894
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56002
diff
changeset
|
340 raise RuntimeError("cannot notify on un-acquired lock") |
|
82594
79943dee0aca
Improve variable names
Raymond Hettinger <python@rcn.com>
parents:
82592
diff
changeset
|
341 all_waiters = self._waiters |
|
79943dee0aca
Improve variable names
Raymond Hettinger <python@rcn.com>
parents:
82592
diff
changeset
|
342 waiters_to_notify = _deque(_islice(all_waiters, n)) |
|
79943dee0aca
Improve variable names
Raymond Hettinger <python@rcn.com>
parents:
82592
diff
changeset
|
343 if not waiters_to_notify: |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 return |
|
82594
79943dee0aca
Improve variable names
Raymond Hettinger <python@rcn.com>
parents:
82592
diff
changeset
|
345 for waiter in waiters_to_notify: |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
346 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
|
347 try: |
|
82594
79943dee0aca
Improve variable names
Raymond Hettinger <python@rcn.com>
parents:
82592
diff
changeset
|
348 all_waiters.remove(waiter) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 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
|
350 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
352 def notify_all(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
353 """Wake up all threads waiting on this condition. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
354 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
355 If the calling thread has not acquired the lock when this method |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
356 is called, a RuntimeError is raised. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
357 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
358 """ |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
359 self.notify(len(self._waiters)) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
361 notifyAll = notify_all |
|
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
362 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
364 class Semaphore: |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
365 """This class implements semaphore objects. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
366 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
367 Semaphores manage a counter representing the number of release() calls minus |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
368 the number of acquire() calls, plus an initial value. The acquire() method |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
369 blocks if necessary until it can return without making the counter |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
370 negative. If not given, value defaults to 1. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
371 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
372 """ |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 |
| 11876 | 374 # 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
|
375 |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
376 def __init__(self, value=1): |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
377 if value < 0: |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
378 raise ValueError("semaphore initial value must be >= 0") |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
379 self._cond = Condition(Lock()) |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
380 self._value = 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
|
381 |
|
60665
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
382 def acquire(self, blocking=True, timeout=None): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
383 """Acquire a semaphore, decrementing the internal counter by one. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
384 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
385 When invoked without arguments: if the internal counter is larger than |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
386 zero on entry, decrement it by one and return immediately. If it is zero |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
387 on entry, block, waiting until some other thread has called release() to |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
388 make it larger than zero. This is done with proper interlocking so that |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
389 if multiple acquire() calls are blocked, release() will wake exactly one |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
390 of them up. The implementation may pick one at random, so the order in |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
391 which blocked threads are awakened should not be relied on. There is no |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
392 return value in this case. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
393 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
394 When invoked with blocking set to true, do the same thing as when called |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
395 without arguments, and return true. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
396 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
397 When invoked with blocking set to false, do not block. If a call without |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
398 an argument would block, return false immediately; otherwise, do the |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
399 same thing as when called without arguments, and return true. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
400 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
401 When invoked with a timeout other than None, it will block for at |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
402 most timeout seconds. If acquire does not complete successfully in |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
403 that interval, return false. Return true otherwise. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
404 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
405 """ |
|
60665
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
406 if not blocking and timeout is not None: |
|
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
407 raise ValueError("can't specify timeout for non-blocking acquire") |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
408 rc = False |
|
60665
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
409 endtime = None |
|
83497
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
410 with self._cond: |
|
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
411 while self._value == 0: |
|
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
412 if not blocking: |
|
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
413 break |
|
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
414 if timeout is not None: |
|
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
415 if endtime is None: |
|
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
416 endtime = _time() + timeout |
|
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
417 else: |
|
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
418 timeout = endtime - _time() |
|
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
419 if timeout <= 0: |
|
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
420 break |
|
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
421 self._cond.wait(timeout) |
|
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
422 else: |
|
83498
a26df2d03989
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
diff
changeset
|
423 self._value -= 1 |
|
83497
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
424 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
|
425 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
|
426 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
427 __enter__ = acquire |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
428 |
|
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 def release(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
430 """Release a semaphore, incrementing the internal counter by one. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
431 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
432 When the counter is zero on entry and another thread is waiting for it |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
433 to become larger than zero again, wake up that thread. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
434 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
435 """ |
|
83497
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
436 with self._cond: |
|
83498
a26df2d03989
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
diff
changeset
|
437 self._value += 1 |
|
83497
af30c5cb248f
Issue #11714: Use 'with' statements to assure a Semaphore releases a
Serhiy Storchaka <storchaka@gmail.com>
parents:
83029
diff
changeset
|
438 self._cond.notify() |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
439 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
440 def __exit__(self, t, v, tb): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
441 self.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
442 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
443 |
|
71560
7f606223578a
Remove indirection in threading (issue #10968).
Éric Araujo <merwok@netwok.org>
parents:
71362
diff
changeset
|
444 class BoundedSemaphore(Semaphore): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
445 """Implements a bounded semaphore. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
446 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
447 A bounded semaphore checks to make sure its current value doesn't exceed its |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
448 initial value. If it does, ValueError is raised. In most situations |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
449 semaphores are used to guard resources with limited capacity. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
450 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
451 If the semaphore is released too many times it's a sign of a bug. If not |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
452 given, value defaults to 1. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
453 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
454 Like regular semaphores, bounded semaphores manage a counter representing |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
455 the number of release() calls minus the number of acquire() calls, plus an |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
456 initial value. The acquire() method blocks if necessary until it can return |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
457 without making the counter negative. If not given, value defaults to 1. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
458 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
459 """ |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
460 |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
461 def __init__(self, value=1): |
|
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
462 Semaphore.__init__(self, value) |
|
19404
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
463 self._initial_value = value |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
464 |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
465 def release(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
466 """Release a semaphore, incrementing the internal counter by one. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
467 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
468 When the counter is zero on entry and another thread is waiting for it |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
469 to become larger than zero again, wake up that thread. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
470 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
471 If the number of releases exceeds the number of acquires, |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
472 raise a ValueError. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
473 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
474 """ |
|
86165
e06edc0c7a49
Issue 19158: a rare race in BoundedSemaphore could allow .release() too often.
Tim Peters <tim@python.org>
parents:
85463
diff
changeset
|
475 with self._cond: |
|
e06edc0c7a49
Issue 19158: a rare race in BoundedSemaphore could allow .release() too often.
Tim Peters <tim@python.org>
parents:
85463
diff
changeset
|
476 if self._value >= self._initial_value: |
|
e06edc0c7a49
Issue 19158: a rare race in BoundedSemaphore could allow .release() too often.
Tim Peters <tim@python.org>
parents:
85463
diff
changeset
|
477 raise ValueError("Semaphore released too many times") |
|
e06edc0c7a49
Issue 19158: a rare race in BoundedSemaphore could allow .release() too often.
Tim Peters <tim@python.org>
parents:
85463
diff
changeset
|
478 self._value += 1 |
|
e06edc0c7a49
Issue 19158: a rare race in BoundedSemaphore could allow .release() too often.
Tim Peters <tim@python.org>
parents:
85463
diff
changeset
|
479 self._cond.notify() |
|
19404
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
480 |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
481 |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
482 class Event: |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
483 """Class implementing event objects. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
484 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
485 Events manage a flag that can be set to true with the set() method and reset |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
486 to false with the clear() method. The wait() method blocks until the flag is |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
487 true. The flag is initially false. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
488 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
489 """ |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
490 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
491 # 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
|
492 |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
493 def __init__(self): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
494 self._cond = Condition(Lock()) |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
495 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
|
496 |
|
67355
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
497 def _reset_internal_locks(self): |
|
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
498 # private! called by Thread._reset_internal_locks by _after_fork() |
|
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
499 self._cond.__init__() |
|
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
500 |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
501 def is_set(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
502 """Return true if and only if the internal flag is true.""" |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
503 return self._flag |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
504 |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
505 isSet = is_set |
|
48757
54138ee20e57
add full deprecation warnings for old threading APIs
Benjamin Peterson <benjamin@python.org>
parents:
48748
diff
changeset
|
506 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
507 def set(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
508 """Set the internal flag to true. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
509 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
510 All threads waiting for it to become true are awakened. Threads |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
511 that call wait() once the flag is true will not block at all. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
512 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
513 """ |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
514 self._cond.acquire() |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
515 try: |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
516 self._flag = True |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
517 self._cond.notify_all() |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
518 finally: |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
519 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
|
520 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
521 def clear(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
522 """Reset the internal flag to false. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
523 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
524 Subsequently, threads calling wait() will block until set() is called to |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
525 set the internal flag to true again. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
526 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
527 """ |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
528 self._cond.acquire() |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
529 try: |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
530 self._flag = False |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
531 finally: |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
532 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
|
533 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
534 def wait(self, timeout=None): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
535 """Block until the internal flag is true. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
536 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
537 If the internal flag is true on entry, return immediately. Otherwise, |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
538 block until another thread calls set() to set the flag to true, or until |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
539 the optional timeout occurs. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
540 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
541 When the timeout argument is present and not None, it should be a |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
542 floating point number specifying a timeout for the operation in seconds |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
543 (or fractions thereof). |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
544 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
545 This method returns the internal flag on exit, so it will always return |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
546 True except if a timeout is given and the operation times out. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
547 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
548 """ |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
549 self._cond.acquire() |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
550 try: |
|
74292
eb39d862a250
Issue #13502: threading: Fix a race condition in Event.wait() that made it
Charles-François Natali <neologix@free.fr>
parents:
74037
diff
changeset
|
551 signaled = self._flag |
|
eb39d862a250
Issue #13502: threading: Fix a race condition in Event.wait() that made it
Charles-François Natali <neologix@free.fr>
parents:
74037
diff
changeset
|
552 if not signaled: |
|
eb39d862a250
Issue #13502: threading: Fix a race condition in Event.wait() that made it
Charles-François Natali <neologix@free.fr>
parents:
74037
diff
changeset
|
553 signaled = self._cond.wait(timeout) |
|
eb39d862a250
Issue #13502: threading: Fix a race condition in Event.wait() that made it
Charles-François Natali <neologix@free.fr>
parents:
74037
diff
changeset
|
554 return signaled |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
555 finally: |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
556 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
|
557 |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
558 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
559 # A barrier class. Inspired in part by the pthread_barrier_* api and |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
560 # the CyclicBarrier class from Java. See |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
561 # http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
562 # http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
563 # CyclicBarrier.html |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
564 # for information. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
565 # We maintain two main states, 'filling' and 'draining' enabling the barrier |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
566 # to be cyclic. Threads are not allowed into it until it has fully drained |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
567 # since the previous cycle. In addition, a 'resetting' state exists which is |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
568 # similar to 'draining' except that threads leave with a BrokenBarrierError, |
|
72985
e19ccbbe70dd
Remove duplication.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
71361
diff
changeset
|
569 # and a 'broken' state in which all threads get the exception. |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
570 class Barrier: |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
571 """Implements a Barrier. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
572 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
573 Useful for synchronizing a fixed number of threads at known synchronization |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
574 points. Threads block on 'wait()' and are simultaneously once they have all |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
575 made that call. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
576 |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
577 """ |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
578 |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
579 def __init__(self, parties, action=None, timeout=None): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
580 """Create a barrier, initialised to 'parties' threads. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
581 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
582 'action' is a callable which, when supplied, will be called by one of |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
583 the threads after they have all entered the barrier and just prior to |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
584 releasing them all. If a 'timeout' is provided, it is uses as the |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
585 default for all subsequent 'wait()' calls. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
586 |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
587 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
588 self._cond = Condition(Lock()) |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
589 self._action = action |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
590 self._timeout = timeout |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
591 self._parties = parties |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
592 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
593 self._count = 0 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
594 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
595 def wait(self, timeout=None): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
596 """Wait for the barrier. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
597 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
598 When the specified number of threads have started waiting, they are all |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
599 simultaneously awoken. If an 'action' was provided for the barrier, one |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
600 of the threads will have executed that callback prior to returning. |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
601 Returns an individual index number from 0 to 'parties-1'. |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
602 |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
603 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
604 if timeout is None: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
605 timeout = self._timeout |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
606 with self._cond: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
607 self._enter() # Block while the barrier drains. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
608 index = self._count |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
609 self._count += 1 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
610 try: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
611 if index + 1 == self._parties: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
612 # We release the barrier |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
613 self._release() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
614 else: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
615 # We wait until someone releases us |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
616 self._wait(timeout) |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
617 return index |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
618 finally: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
619 self._count -= 1 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
620 # Wake up any threads waiting for barrier to drain. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
621 self._exit() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
622 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
623 # Block until the barrier is ready for us, or raise an exception |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
624 # if it is broken. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
625 def _enter(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
626 while self._state in (-1, 1): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
627 # It is draining or resetting, wait until done |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
628 self._cond.wait() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
629 #see if the barrier is in a broken state |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
630 if self._state < 0: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
631 raise BrokenBarrierError |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
632 assert self._state == 0 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
633 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
634 # Optionally run the 'action' and release the threads waiting |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
635 # in the barrier. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
636 def _release(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
637 try: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
638 if self._action: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
639 self._action() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
640 # enter draining state |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
641 self._state = 1 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
642 self._cond.notify_all() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
643 except: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
644 #an exception during the _action handler. Break and reraise |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
645 self._break() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
646 raise |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
647 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
648 # Wait in the barrier until we are relased. Raise an exception |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
649 # if the barrier is reset or broken. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
650 def _wait(self, timeout): |
|
66229
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
651 if not self._cond.wait_for(lambda : self._state != 0, timeout): |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
652 #timed out. Break the barrier |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
653 self._break() |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
654 raise BrokenBarrierError |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
655 if self._state < 0: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
656 raise BrokenBarrierError |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
657 assert self._state == 1 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
658 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
659 # If we are the last thread to exit the barrier, signal any threads |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
660 # waiting for the barrier to drain. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
661 def _exit(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
662 if self._count == 0: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
663 if self._state in (-1, 1): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
664 #resetting or draining |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
665 self._state = 0 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
666 self._cond.notify_all() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
667 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
668 def reset(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
669 """Reset the barrier to the initial state. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
670 |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
671 Any threads currently waiting will get the BrokenBarrier exception |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
672 raised. |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
673 |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
674 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
675 with self._cond: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
676 if self._count > 0: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
677 if self._state == 0: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
678 #reset the barrier, waking up threads |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
679 self._state = -1 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
680 elif self._state == -2: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
681 #was broken, set it to reset state |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
682 #which clears when the last thread exits |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
683 self._state = -1 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
684 else: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
685 self._state = 0 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
686 self._cond.notify_all() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
687 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
688 def abort(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
689 """Place the barrier into a 'broken' state. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
690 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
691 Useful in case of error. Any currently waiting threads and threads |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
692 attempting to 'wait()' will have BrokenBarrierError raised. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
693 |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
694 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
695 with self._cond: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
696 self._break() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
697 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
698 def _break(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
699 # An internal error was detected. The barrier is set to |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
700 # a broken state all parties awakened. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
701 self._state = -2 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
702 self._cond.notify_all() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
703 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
704 @property |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
705 def parties(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
706 """Return the number of threads required to trip the barrier.""" |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
707 return self._parties |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
708 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
709 @property |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
710 def n_waiting(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
711 """Return the number of threads currently waiting at the barrier.""" |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
712 # We don't need synchronization here since this is an ephemeral result |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
713 # anyway. It returns the correct value in the steady state. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
714 if self._state == 0: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
715 return self._count |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
716 return 0 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
717 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
718 @property |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
719 def broken(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
720 """Return True if the barrier is in a broken state.""" |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
721 return self._state == -2 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
722 |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
723 # exception raised by the Barrier class |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
724 class BrokenBarrierError(RuntimeError): |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
725 pass |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
726 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
727 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
728 # 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
|
729 _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
|
730 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
|
731 global _counter |
|
82591
28b86525ef07
Update code to increment and decrement using the cleaner += 1 and -= 1 style.
Raymond Hettinger <python@rcn.com>
parents:
76597
diff
changeset
|
732 _counter += 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
|
733 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
|
734 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
735 # 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
|
736 _active_limbo_lock = _allocate_lock() |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
737 _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
|
738 _limbo = {} |
|
71361
2bedea96de60
Issue #12573: Add resource checks for dangling Thread and Process objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
739 _dangling = WeakSet() |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
740 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
741 # 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
|
742 |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
743 class Thread: |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
744 """A class that represents a thread of control. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
745 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
746 This class can be safely subclassed in a limited fashion. There are two ways |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
747 to specify the activity: by passing a callable object to the constructor, or |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
748 by overriding the run() method in a subclass. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
749 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
750 """ |
|
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 |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
752 __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
|
753 # 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
|
754 # 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
|
755 # 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
|
756 # 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
|
757 __exc_info = _sys.exc_info |
|
46161
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45549
diff
changeset
|
758 # Keep sys.exc_clear too to clear the exception just before |
|
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45549
diff
changeset
|
759 # allowing .join() to return. |
|
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45549
diff
changeset
|
760 #XXX __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
|
761 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
762 def __init__(self, group=None, target=None, name=None, |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
763 args=(), kwargs=None, *, daemon=None): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
764 """This constructor should always be called with keyword arguments. Arguments are: |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
765 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
766 *group* should be None; reserved for future extension when a ThreadGroup |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
767 class is implemented. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
768 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
769 *target* is the callable object to be invoked by the run() |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
770 method. Defaults to None, meaning nothing is called. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
771 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
772 *name* is the thread name. By default, a unique name is constructed of |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
773 the form "Thread-N" where N is a small decimal number. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
774 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
775 *args* is the argument tuple for the target invocation. Defaults to (). |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
776 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
777 *kwargs* is a dictionary of keyword arguments for the target |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
778 invocation. Defaults to {}. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
779 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
780 If a subclass overrides the constructor, it must make sure to invoke |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
781 the base class constructor (Thread.__init__()) before doing anything |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
782 else to the thread. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
783 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
784 """ |
|
8474
049aa3a8a172
Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents:
8454
diff
changeset
|
785 assert group is None, "group argument must be None for now" |
|
35236
d0c6965b525e
bug [ 1238170 ] threading.Thread uses {} as default argument
Georg Brandl <georg@python.org>
parents:
34512
diff
changeset
|
786 if kwargs is None: |
|
d0c6965b525e
bug [ 1238170 ] threading.Thread uses {} as default argument
Georg Brandl <georg@python.org>
parents:
34512
diff
changeset
|
787 kwargs = {} |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
788 self._target = target |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
789 self._name = str(name or _newname()) |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
790 self._args = args |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
791 self._kwargs = kwargs |
|
68155
f71acc4b2341
Issue #6064: Add a `daemon` keyword argument to the threading.Thread
Antoine Pitrou <solipsis@pitrou.net>
parents:
67629
diff
changeset
|
792 if daemon is not None: |
|
f71acc4b2341
Issue #6064: Add a `daemon` keyword argument to the threading.Thread
Antoine Pitrou <solipsis@pitrou.net>
parents:
67629
diff
changeset
|
793 self._daemonic = daemon |
|
f71acc4b2341
Issue #6064: Add a `daemon` keyword argument to the threading.Thread
Antoine Pitrou <solipsis@pitrou.net>
parents:
67629
diff
changeset
|
794 else: |
|
f71acc4b2341
Issue #6064: Add a `daemon` keyword argument to the threading.Thread
Antoine Pitrou <solipsis@pitrou.net>
parents:
67629
diff
changeset
|
795 self._daemonic = current_thread().daemon |
|
47555
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
796 self._ident = None |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
797 self._tstate_lock = None |
|
45549
8dd30fe11a4b
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61103 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45495
diff
changeset
|
798 self._started = Event() |
|
85631
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
799 self._is_stopped = False |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
800 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
|
801 # 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
|
802 # sys.exc_info since it can be changed between instances |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
803 self._stderr = _sys.stderr |
|
85623
74dc664ad699
Issue #18808 again: fix the after-fork logic for not-yet-started or already-stopped threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85604
diff
changeset
|
804 # For debugging and _after_fork() |
|
71361
2bedea96de60
Issue #12573: Add resource checks for dangling Thread and Process objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
805 _dangling.add(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
|
806 |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
807 def _reset_internal_locks(self, is_alive): |
|
67355
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
808 # private! Called by _after_fork() to reset our internal locks as |
|
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
809 # they may be in an invalid state leading to a deadlock or crash. |
|
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
810 self._started._reset_internal_locks() |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
811 if is_alive: |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
812 self._set_tstate_lock() |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
813 else: |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
814 # The thread isn't alive after fork: it doesn't have a tstate |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
815 # anymore. |
|
85649
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
816 self._is_stopped = True |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
817 self._tstate_lock = None |
|
67355
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
818 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
819 def __repr__(self): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
820 assert self._initialized, "Thread.__init__() was not called" |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
821 status = "initial" |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
822 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
|
823 status = "started" |
|
85650
967af1815967
Get "stopped" back into repr(Thread) when appropriate.
Tim Peters <tim@python.org>
parents:
85649
diff
changeset
|
824 self.is_alive() # easy way to get ._is_stopped set when appropriate |
|
85631
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
825 if self._is_stopped: |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
826 status = "stopped" |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
827 if self._daemonic: |
|
47555
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
828 status += " daemon" |
|
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
829 if self._ident is not None: |
|
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
830 status += " %s" % self._ident |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
831 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
832 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
833 def start(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
834 """Start the thread's activity. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
835 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
836 It must be called at most once per thread object. It arranges for the |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
837 object's run() method to be invoked in a separate thread of control. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
838 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
839 This method will raise a RuntimeError if called more than once on the |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
840 same thread object. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
841 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
842 """ |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
843 if not self._initialized: |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
844 raise RuntimeError("thread.__init__() not called") |
|
45549
8dd30fe11a4b
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61103 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45495
diff
changeset
|
845 |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
846 if self._started.is_set(): |
|
60379
c10024abd118
Merged revisions 79819 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
59203
diff
changeset
|
847 raise RuntimeError("threads can only be started once") |
|
52892
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
848 with _active_limbo_lock: |
|
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
849 _limbo[self] = self |
|
59203
9e8113e76adb
Merged revisions 78519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
57160
diff
changeset
|
850 try: |
|
9e8113e76adb
Merged revisions 78519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
57160
diff
changeset
|
851 _start_new_thread(self._bootstrap, ()) |
|
9e8113e76adb
Merged revisions 78519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
57160
diff
changeset
|
852 except Exception: |
|
9e8113e76adb
Merged revisions 78519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
57160
diff
changeset
|
853 with _active_limbo_lock: |
|
9e8113e76adb
Merged revisions 78519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
57160
diff
changeset
|
854 del _limbo[self] |
|
9e8113e76adb
Merged revisions 78519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
57160
diff
changeset
|
855 raise |
|
45549
8dd30fe11a4b
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61103 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45495
diff
changeset
|
856 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
|
857 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
858 def run(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
859 """Method representing the thread's activity. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
860 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
861 You may override this method in a subclass. The standard run() method |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
862 invokes the callable object passed to the object's constructor as the |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
863 target argument, if any, with sequential and keyword arguments taken |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
864 from the args and kwargs arguments, respectively. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
865 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
866 """ |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44943
diff
changeset
|
867 try: |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44943
diff
changeset
|
868 if self._target: |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44943
diff
changeset
|
869 self._target(*self._args, **self._kwargs) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44943
diff
changeset
|
870 finally: |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44943
diff
changeset
|
871 # Avoid a refcycle if the thread is running a function with |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44943
diff
changeset
|
872 # an argument that has a member that points to the thread. |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44943
diff
changeset
|
873 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
|
874 |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
875 def _bootstrap(self): |
|
42678
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
876 # Wrapper around the real bootstrap code that ignores |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
877 # exceptions during interpreter cleanup. Those typically |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
878 # happen when a daemon thread wakes up at an unfortunate |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
879 # moment, finds the world around it destroyed, and raises some |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
880 # random exception *** while trying to report the exception in |
|
45549
8dd30fe11a4b
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61103 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45495
diff
changeset
|
881 # _bootstrap_inner() below ***. Those random exceptions |
|
42678
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
882 # don't help anybody, and they confuse users, so we suppress |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
883 # them. We suppress them only when it appears that the world |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
884 # indeed has already been destroyed, so that exceptions in |
|
45549
8dd30fe11a4b
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61103 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45495
diff
changeset
|
885 # _bootstrap_inner() during normal business hours are properly |
|
42678
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
886 # reported. Also, we only suppress them for daemonic threads; |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
887 # if a non-daemonic encounters this, something else is wrong. |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
888 try: |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
889 self._bootstrap_inner() |
|
42678
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
890 except: |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
891 if self._daemonic and _sys is None: |
|
42678
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
892 return |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
893 raise |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
894 |
|
52892
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
895 def _set_ident(self): |
|
70530
5548197d1f22
Close #12028: Make threading._get_ident() public, rename it to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69710
diff
changeset
|
896 self._ident = get_ident() |
|
52892
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
897 |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
898 def _set_tstate_lock(self): |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
899 """ |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
900 Set a lock object which will be released by the interpreter when |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
901 the underlying thread state (see pystate.h) gets deleted. |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
902 """ |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
903 self._tstate_lock = _set_sentinel() |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
904 self._tstate_lock.acquire() |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
905 |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
906 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
|
907 try: |
|
52892
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
908 self._set_ident() |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
909 self._set_tstate_lock() |
|
45549
8dd30fe11a4b
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61103 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45495
diff
changeset
|
910 self._started.set() |
|
52892
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
911 with _active_limbo_lock: |
|
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
912 _active[self._ident] = self |
|
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
913 del _limbo[self] |
|
29679
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
914 |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
915 if _trace_hook: |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
916 _sys.settrace(_trace_hook) |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
917 if _profile_hook: |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
918 _sys.setprofile(_profile_hook) |
|
29686
371092ce5c2b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
29683
diff
changeset
|
919 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
920 try: |
|
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.run() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
922 except SystemExit: |
|
75373
8ec51b2e57c2
Close #13550: Remove the debug machinery from the threading module: remove
Victor Stinner <victor.stinner@gmail.com>
parents:
74750
diff
changeset
|
923 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
|
924 except: |
|
32475
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
925 # If sys.stderr is no more (most likely from interpreter |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
926 # shutdown) use self._stderr. Otherwise still use sys (as in |
|
32475
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
927 # _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
|
928 # self. |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
929 if _sys: |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
930 _sys.stderr.write("Exception in thread %s:\n%s\n" % |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
931 (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
|
932 else: |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
933 # 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
|
934 # 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
|
935 # Lib/traceback.py) |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
936 exc_type, exc_value, exc_tb = self._exc_info() |
|
32475
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
937 try: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
938 print(( |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
939 "Exception in thread " + self.name + |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
940 " (most likely raised during interpreter shutdown):"), file=self._stderr) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
941 print(( |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
942 "Traceback (most recent call last):"), file=self._stderr) |
|
32475
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
943 while exc_tb: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
944 print(( |
|
32475
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
945 ' 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
|
946 (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
|
947 exc_tb.tb_lineno, |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
948 exc_tb.tb_frame.f_code.co_name)), file=self._stderr) |
|
32475
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
949 exc_tb = exc_tb.tb_next |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
950 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr) |
|
32475
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
951 # 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
|
952 # 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
|
953 finally: |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
954 del exc_type, exc_value, exc_tb |
|
46161
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45549
diff
changeset
|
955 finally: |
|
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45549
diff
changeset
|
956 # Prevent a race in |
|
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45549
diff
changeset
|
957 # test_threading.test_no_refcycle_through_target when |
|
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45549
diff
changeset
|
958 # the exception keeps the target alive past when we |
|
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45549
diff
changeset
|
959 # assert that it's dead. |
|
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45549
diff
changeset
|
960 #XXX self.__exc_clear() |
|
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45549
diff
changeset
|
961 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
|
962 finally: |
|
44885
fefaa0e66fdd
Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
963 with _active_limbo_lock: |
|
fefaa0e66fdd
Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
964 try: |
|
47555
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
965 # We don't call self._delete() because it also |
|
44885
fefaa0e66fdd
Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
966 # grabs _active_limbo_lock. |
|
70530
5548197d1f22
Close #12028: Make threading._get_ident() public, rename it to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69710
diff
changeset
|
967 del _active[get_ident()] |
|
44885
fefaa0e66fdd
Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
968 except: |
|
fefaa0e66fdd
Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
969 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
|
970 |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
971 def _stop(self): |
|
85649
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
972 # After calling ._stop(), .is_alive() returns False and .join() returns |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
973 # immediately. ._tstate_lock must be released before calling ._stop(). |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
974 # |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
975 # Normal case: C code at the end of the thread's life |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
976 # (release_sentinel in _threadmodule.c) releases ._tstate_lock, and |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
977 # that's detected by our ._wait_for_tstate_lock(), called by .join() |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
978 # and .is_alive(). Any number of threads _may_ call ._stop() |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
979 # simultaneously (for example, if multiple threads are blocked in |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
980 # .join() calls), and they're not serialized. That's harmless - |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
981 # they'll just make redundant rebindings of ._is_stopped and |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
982 # ._tstate_lock. Obscure: we rebind ._tstate_lock last so that the |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
983 # "assert self._is_stopped" in ._wait_for_tstate_lock() always works |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
984 # (the assert is executed only if ._tstate_lock is None). |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
985 # |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
986 # Special case: _main_thread releases ._tstate_lock via this |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
987 # module's _shutdown() function. |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
988 lock = self._tstate_lock |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
989 if lock is not None: |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
990 assert not lock.locked() |
|
85648
e61780cd2c78
Backed out changeset 1f5a7853680c
Tim Peters <tim@python.org>
parents:
85647
diff
changeset
|
991 self._is_stopped = True |
|
e61780cd2c78
Backed out changeset 1f5a7853680c
Tim Peters <tim@python.org>
parents:
85647
diff
changeset
|
992 self._tstate_lock = None |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
993 |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
994 def _delete(self): |
|
32778
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
995 "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
|
996 |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
997 # Notes about running with _dummy_thread: |
|
32778
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
998 # |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
999 # Must take care to not raise an exception if _dummy_thread is being |
|
32778
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
1000 # used (and thus this module is being used as an instance of |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
1001 # dummy_threading). _dummy_thread.get_ident() always returns -1 since |
|
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
1002 # there is only one thread if _dummy_thread is being used. Thus |
|
32778
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
1003 # 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
|
1004 # 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
|
1005 # |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
1006 # 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
|
1007 # gets overwritten the instant an instance of Thread is created; both |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
1008 # threads return -1 from _dummy_thread.get_ident() and thus have the |
|
32778
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
1009 # 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
|
1010 # '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
|
1011 # 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
|
1012 # |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
1013 # 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
|
1014 # _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
|
1015 # 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
|
1016 # 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
|
1017 |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
1018 try: |
|
46304
3442e9e6b2cf
Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62055-62058,62060-62066,62068-62074,62076-62079,62081-62083,62086-62089,62092-62094,62098,62101,62104,62106-62109,62115-62122,62124-62125,62127-62128,62130,62132,62134-62137,62139-62142,62144,62146-62148,62150-62152,62155-62161 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents:
46161
diff
changeset
|
1019 with _active_limbo_lock: |
|
70530
5548197d1f22
Close #12028: Make threading._get_ident() public, rename it to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69710
diff
changeset
|
1020 del _active[get_ident()] |
|
46304
3442e9e6b2cf
Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62055-62058,62060-62066,62068-62074,62076-62079,62081-62083,62086-62089,62092-62094,62098,62101,62104,62106-62109,62115-62122,62124-62125,62127-62128,62130,62132,62134-62137,62139-62142,62144,62146-62148,62150-62152,62155-62161 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents:
46161
diff
changeset
|
1021 # There must not be any python code between the previous line |
|
3442e9e6b2cf
Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62055-62058,62060-62066,62068-62074,62076-62079,62081-62083,62086-62089,62092-62094,62098,62101,62104,62106-62109,62115-62122,62124-62125,62127-62128,62130,62132,62134-62137,62139-62142,62144,62146-62148,62150-62152,62155-62161 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents:
46161
diff
changeset
|
1022 # and after the lock is released. Otherwise a tracing function |
|
3442e9e6b2cf
Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62055-62058,62060-62066,62068-62074,62076-62079,62081-62083,62086-62089,62092-62094,62098,62101,62104,62106-62109,62115-62122,62124-62125,62127-62128,62130,62132,62134-62137,62139-62142,62144,62146-62148,62150-62152,62155-62161 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents:
46161
diff
changeset
|
1023 # could try to acquire the lock again in the same thread, (in |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1024 # current_thread()), and would block. |
|
46304
3442e9e6b2cf
Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62055-62058,62060-62066,62068-62074,62076-62079,62081-62083,62086-62089,62092-62094,62098,62101,62104,62106-62109,62115-62122,62124-62125,62127-62128,62130,62132,62134-62137,62139-62142,62144,62146-62148,62150-62152,62155-62161 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents:
46161
diff
changeset
|
1025 except KeyError: |
|
3442e9e6b2cf
Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62055-62058,62060-62066,62068-62074,62076-62079,62081-62083,62086-62089,62092-62094,62098,62101,62104,62106-62109,62115-62122,62124-62125,62127-62128,62130,62132,62134-62137,62139-62142,62144,62146-62148,62150-62152,62155-62161 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents:
46161
diff
changeset
|
1026 if 'dummy_threading' not in _sys.modules: |
|
3442e9e6b2cf
Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62055-62058,62060-62066,62068-62074,62076-62079,62081-62083,62086-62089,62092-62094,62098,62101,62104,62106-62109,62115-62122,62124-62125,62127-62128,62130,62132,62134-62137,62139-62142,62144,62146-62148,62150-62152,62155-62161 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents:
46161
diff
changeset
|
1027 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
|
1028 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1029 def join(self, timeout=None): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1030 """Wait until the thread terminates. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1031 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1032 This blocks the calling thread until the thread whose join() method is |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1033 called terminates -- either normally or through an unhandled exception |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1034 or until the optional timeout occurs. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1035 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1036 When the timeout argument is present and not None, it should be a |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1037 floating point number specifying a timeout for the operation in seconds |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1038 (or fractions thereof). As join() always returns None, you must call |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1039 isAlive() after join() to decide whether a timeout happened -- if the |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1040 thread is still alive, the join() call timed out. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1041 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1042 When the timeout argument is not present or None, the operation will |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1043 block until the thread terminates. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1044 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1045 A thread can be join()ed many times. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1046 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1047 join() raises a RuntimeError if an attempt is made to join the current |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1048 thread as that would cause a deadlock. It is also an error to join() a |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1049 thread before it has been started and attempts to do so raises the same |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1050 exception. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1051 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1052 """ |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
1053 if not self._initialized: |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1054 raise RuntimeError("Thread.__init__() not called") |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1055 if not self._started.is_set(): |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1056 raise RuntimeError("cannot join thread before it is started") |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1057 if self is current_thread(): |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1058 raise RuntimeError("cannot join current thread") |
|
86643
af67cfcd4089
Issue #19399: fix sporadic test_subprocess failure.
Tim Peters <tim@python.org>
parents:
86293
diff
changeset
|
1059 |
|
85631
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1060 if timeout is None: |
|
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1061 self._wait_for_tstate_lock() |
|
86644
506a07ea1173
Fiddled Thread.join() to be a little simpler. Kinda ;-)
Tim Peters <tim@python.org>
parents:
86643
diff
changeset
|
1062 else: |
|
506a07ea1173
Fiddled Thread.join() to be a little simpler. Kinda ;-)
Tim Peters <tim@python.org>
parents:
86643
diff
changeset
|
1063 # the behavior of a negative timeout isn't documented, but |
|
86659
86a855ee0456
Changed a comment to end grammar bikeshedding ;-)
Tim Peters <tim@python.org>
parents:
86644
diff
changeset
|
1064 # historically .join(timeout=x) for x<0 has acted as if timeout=0 |
|
86644
506a07ea1173
Fiddled Thread.join() to be a little simpler. Kinda ;-)
Tim Peters <tim@python.org>
parents:
86643
diff
changeset
|
1065 self._wait_for_tstate_lock(timeout=max(timeout, 0)) |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1066 |
|
85631
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1067 def _wait_for_tstate_lock(self, block=True, timeout=-1): |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
1068 # Issue #18808: wait for the thread state to be gone. |
|
85631
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1069 # At the end of the thread's life, after all knowledge of the thread |
|
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1070 # is removed from C data structures, C code releases our _tstate_lock. |
|
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1071 # This method passes its arguments to _tstate_lock.aquire(). |
|
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1072 # If the lock is acquired, the C code is done, and self._stop() is |
|
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1073 # called. That sets ._is_stopped to True, and ._tstate_lock to None. |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
1074 lock = self._tstate_lock |
|
85631
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1075 if lock is None: # already determined that the C code is done |
|
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1076 assert self._is_stopped |
|
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1077 elif lock.acquire(block, timeout): |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
1078 lock.release() |
|
85631
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1079 self._stop() |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1080 |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
1081 @property |
|
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
1082 def name(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1083 """A string used for identification purposes only. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1084 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1085 It has no semantics. Multiple threads may be given the same name. The |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1086 initial name is set by the constructor. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1087 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1088 """ |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
1089 assert self._initialized, "Thread.__init__() not called" |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
1090 return 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
|
1091 |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
1092 @name.setter |
|
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
1093 def name(self, name): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
1094 assert self._initialized, "Thread.__init__() not called" |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
1095 self._name = str(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
|
1096 |
|
48743
76620010d782
Merged revisions 65818 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48516
diff
changeset
|
1097 @property |
|
76620010d782
Merged revisions 65818 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48516
diff
changeset
|
1098 def ident(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1099 """Thread identifier of this thread or None if it has not been started. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1100 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1101 This is a nonzero integer. See the thread.get_ident() function. Thread |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1102 identifiers may be recycled when a thread exits and another thread is |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1103 created. The identifier is available even after the thread has exited. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1104 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1105 """ |
|
47555
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
1106 assert self._initialized, "Thread.__init__() not called" |
|
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
1107 return self._ident |
|
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47266
diff
changeset
|
1108 |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1109 def is_alive(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1110 """Return whether the thread is alive. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1111 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1112 This method returns True just before the run() method starts until just |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1113 after the run() method terminates. The module function enumerate() |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1114 returns a list of all alive threads. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1115 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1116 """ |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
1117 assert self._initialized, "Thread.__init__() not called" |
|
85631
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1118 if self._is_stopped or not self._started.is_set(): |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
1119 return False |
|
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
1120 self._wait_for_tstate_lock(False) |
|
85631
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1121 return not self._is_stopped |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15586
diff
changeset
|
1122 |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
1123 isAlive = is_alive |
|
48757
54138ee20e57
add full deprecation warnings for old threading APIs
Benjamin Peterson <benjamin@python.org>
parents:
48748
diff
changeset
|
1124 |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
1125 @property |
|
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
1126 def daemon(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1127 """A boolean value indicating whether this thread is a daemon thread. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1128 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1129 This must be set before start() is called, otherwise RuntimeError is |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1130 raised. Its initial value is inherited from the creating thread; the |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1131 main thread is not a daemon thread and therefore all threads created in |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1132 the main thread default to daemon = False. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1133 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1134 The entire Python program exits when no alive non-daemon threads are |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1135 left. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1136 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1137 """ |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
1138 assert self._initialized, "Thread.__init__() not called" |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
1139 return self._daemonic |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1140 |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
1141 @daemon.setter |
|
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
1142 def daemon(self, daemonic): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
1143 if not self._initialized: |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1144 raise RuntimeError("Thread.__init__() not called") |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1145 if self._started.is_set(): |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1146 raise RuntimeError("cannot set daemon status of active thread"); |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
1147 self._daemonic = daemonic |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1148 |
|
48748
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
1149 def isDaemon(self): |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
1150 return self.daemon |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
1151 |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
1152 def setDaemon(self, daemonic): |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
1153 self.daemon = daemonic |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
1154 |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
1155 def getName(self): |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
1156 return self.name |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
1157 |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
1158 def setName(self, name): |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
1159 self.name = name |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
1160 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
1161 # 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
|
1162 |
|
71560
7f606223578a
Remove indirection in threading (issue #10968).
Éric Araujo <merwok@netwok.org>
parents:
71362
diff
changeset
|
1163 class Timer(Thread): |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
1164 """Call a function after a specified number of seconds: |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
1165 |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1166 t = Timer(30.0, f, args=None, kwargs=None) |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1167 t.start() |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1168 t.cancel() # stop the timer's action if it's still waiting |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1169 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
1170 """ |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
1171 |
|
83029
2698920eadcd
Issue #17435: Don't use mutable default values in Timer.
R David Murray <rdmurray@bitdance.com>
parents:
76597
diff
changeset
|
1172 def __init__(self, interval, function, args=None, kwargs=None): |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
1173 Thread.__init__(self) |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
1174 self.interval = interval |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
1175 self.function = function |
|
83029
2698920eadcd
Issue #17435: Don't use mutable default values in Timer.
R David Murray <rdmurray@bitdance.com>
parents:
76597
diff
changeset
|
1176 self.args = args if args is not None else [] |
|
2698920eadcd
Issue #17435: Don't use mutable default values in Timer.
R David Murray <rdmurray@bitdance.com>
parents:
76597
diff
changeset
|
1177 self.kwargs = kwargs if kwargs is not None else {} |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
1178 self.finished = Event() |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
1179 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
1180 def cancel(self): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1181 """Stop the timer if it hasn't finished yet.""" |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
1182 self.finished.set() |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
1183 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
1184 def run(self): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
1185 self.finished.wait(self.interval) |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1186 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
|
1187 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
|
1188 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
|
1189 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1190 # 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
|
1191 # 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
|
1192 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1193 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
|
1194 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1195 def __init__(self): |
|
68155
f71acc4b2341
Issue #6064: Add a `daemon` keyword argument to the threading.Thread
Antoine Pitrou <solipsis@pitrou.net>
parents:
67629
diff
changeset
|
1196 Thread.__init__(self, name="MainThread", daemon=False) |
|
85631
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1197 self._set_tstate_lock() |
|
45549
8dd30fe11a4b
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61103 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45495
diff
changeset
|
1198 self._started.set() |
|
52892
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
1199 self._set_ident() |
|
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
1200 with _active_limbo_lock: |
|
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
1201 _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
|
1202 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1203 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1204 # 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
|
1205 # These aren't garbage collected when they die, nor can they be waited for. |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1206 # 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
|
1207 # leave an entry in the _active dict forever after. |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1208 # 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
|
1209 # 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
|
1210 # 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
|
1211 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1212 class _DummyThread(Thread): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15586
diff
changeset
|
1213 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1214 def __init__(self): |
|
68155
f71acc4b2341
Issue #6064: Add a `daemon` keyword argument to the threading.Thread
Antoine Pitrou <solipsis@pitrou.net>
parents:
67629
diff
changeset
|
1215 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True) |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
1216 |
|
45549
8dd30fe11a4b
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61103 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45495
diff
changeset
|
1217 self._started.set() |
|
52892
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
1218 self._set_ident() |
|
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
1219 with _active_limbo_lock: |
|
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
1220 _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
|
1221 |
|
76421
41c64c700e1e
Issue #14308: Fix an exception when a "dummy" thread is in the threading module's active list after a fork().
Antoine Pitrou <solipsis@pitrou.net>
parents:
74749
diff
changeset
|
1222 def _stop(self): |
|
41c64c700e1e
Issue #14308: Fix an exception when a "dummy" thread is in the threading module's active list after a fork().
Antoine Pitrou <solipsis@pitrou.net>
parents:
74749
diff
changeset
|
1223 pass |
|
41c64c700e1e
Issue #14308: Fix an exception when a "dummy" thread is in the threading module's active list after a fork().
Antoine Pitrou <solipsis@pitrou.net>
parents:
74749
diff
changeset
|
1224 |
|
21981
4e9cc26e03fd
SF #515023. Make _DummyThread.join() signature match base class (Thread)
Neal Norwitz <nnorwitz@gmail.com>
parents:
21589
diff
changeset
|
1225 def join(self, timeout=None): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
1226 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
|
1227 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1228 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1229 # 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
|
1230 |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1231 def current_thread(): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1232 """Return the current Thread object, corresponding to the caller's thread of control. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1233 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1234 If the caller's thread of control was not created through the threading |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1235 module, a dummy thread object with limited functionality is returned. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1236 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1237 """ |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1238 try: |
|
70530
5548197d1f22
Close #12028: Make threading._get_ident() public, rename it to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69710
diff
changeset
|
1239 return _active[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
|
1240 except KeyError: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1241 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
|
1242 |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
1243 currentThread = current_thread |
|
48757
54138ee20e57
add full deprecation warnings for old threading APIs
Benjamin Peterson <benjamin@python.org>
parents:
48748
diff
changeset
|
1244 |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1245 def active_count(): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1246 """Return the number of Thread objects currently alive. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1247 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1248 The returned count is equal to the length of the list returned by |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1249 enumerate(). |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1250 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1251 """ |
|
52892
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
1252 with _active_limbo_lock: |
|
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
1253 return len(_active) + len(_limbo) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1254 |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
1255 activeCount = active_count |
|
48757
54138ee20e57
add full deprecation warnings for old threading APIs
Benjamin Peterson <benjamin@python.org>
parents:
48748
diff
changeset
|
1256 |
|
57121
4ea0a80ed76a
Merged revisions 76119 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56894
diff
changeset
|
1257 def _enumerate(): |
|
4ea0a80ed76a
Merged revisions 76119 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56894
diff
changeset
|
1258 # Same as enumerate(), but without the lock. Internal use only. |
|
4ea0a80ed76a
Merged revisions 76119 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56894
diff
changeset
|
1259 return list(_active.values()) + list(_limbo.values()) |
|
4ea0a80ed76a
Merged revisions 76119 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56894
diff
changeset
|
1260 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1261 def enumerate(): |
|
86292
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1262 """Return a list of all Thread objects currently alive. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1263 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1264 The list includes daemonic threads, dummy thread objects created by |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1265 current_thread(), and the main thread. It excludes terminated threads and |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1266 threads that have not yet been started. |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1267 |
|
24429118988b
Closes #17375: port new threading docstrings from 2.7.
Georg Brandl <georg@python.org>
parents:
86165
diff
changeset
|
1268 """ |
|
52892
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
1269 with _active_limbo_lock: |
|
792bb885029f
Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49058
diff
changeset
|
1270 return list(_active.values()) + list(_limbo.values()) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1271 |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
1272 from _thread import stack_size |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1273 |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1274 # Create the main thread object, |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1275 # and make it available for the interpreter |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1276 # (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
|
1277 |
|
85520
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1278 _main_thread = _MainThread() |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1279 |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1280 def _shutdown(): |
|
85631
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1281 # Obscure: other threads may be waiting to join _main_thread. That's |
|
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1282 # dubious, but some code does it. We can't wait for C code to release |
|
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1283 # the main thread's tstate_lock - that won't happen until the interpreter |
|
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1284 # is nearly dead. So we release it here. Note that just calling _stop() |
|
aff959a3ba13
Issue 18984: Remove ._stopped Event from Thread internals.
Tim Peters <tim@python.org>
parents:
85623
diff
changeset
|
1285 # isn't enough: other threads may already be waiting on _tstate_lock. |
|
85649
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
1286 tlock = _main_thread._tstate_lock |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
1287 # The main thread isn't finished yet, so its thread state lock can't have |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
1288 # been released. |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
1289 assert tlock is not None |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
1290 assert tlock.locked() |
|
eac63e7ceb03
Another stab at the thread cleanup patch.
Tim Peters <tim@python.org>
parents:
85648
diff
changeset
|
1291 tlock.release() |
|
85520
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1292 _main_thread._stop() |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1293 t = _pickSomeNonDaemonThread() |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1294 while t: |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1295 t.join() |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1296 t = _pickSomeNonDaemonThread() |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1297 _main_thread._delete() |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1298 |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1299 def _pickSomeNonDaemonThread(): |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1300 for t in enumerate(): |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1301 if not t.daemon and t.is_alive(): |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1302 return t |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1303 return None |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1304 |
|
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1305 def main_thread(): |
|
85521
f18fb7f39c24
Add docstring for threading.main_thread().
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85520
diff
changeset
|
1306 """Return the main thread object. |
|
f18fb7f39c24
Add docstring for threading.main_thread().
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85520
diff
changeset
|
1307 |
|
f18fb7f39c24
Add docstring for threading.main_thread().
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85520
diff
changeset
|
1308 In normal conditions, the main thread is the thread from which the |
|
f18fb7f39c24
Add docstring for threading.main_thread().
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85520
diff
changeset
|
1309 Python interpreter was started. |
|
f18fb7f39c24
Add docstring for threading.main_thread().
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85520
diff
changeset
|
1310 """ |
|
85520
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1311 return _main_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
|
1312 |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
1313 # 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
|
1314 # 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
|
1315 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
1316 try: |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
1317 from _thread import _local as local |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
1318 except ImportError: |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
1319 from _threading_local import local |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
1320 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1321 |
| 48516 | 1322 def _after_fork(): |
| 1323 # This function is called by Python/ceval.c:PyEval_ReInitThreads which | |
| 1324 # is called from PyOS_AfterFork. Here we cleanup threading module state | |
| 1325 # that should not exist after a fork. | |
| 1326 | |
| 1327 # Reset _active_limbo_lock, in case we forked while the lock was held | |
| 1328 # by another (non-forked) thread. http://bugs.python.org/issue874900 | |
|
85520
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1329 global _active_limbo_lock, _main_thread |
| 48516 | 1330 _active_limbo_lock = _allocate_lock() |
| 1331 | |
| 1332 # fork() only copied the current thread; clear references to others. | |
| 1333 new_active = {} | |
| 1334 current = current_thread() | |
|
85520
96e55a1a0de7
Issue #18882: Add threading.main_thread() function.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
85464
diff
changeset
|
1335 _main_thread = current |
| 48516 | 1336 with _active_limbo_lock: |
|
85623
74dc664ad699
Issue #18808 again: fix the after-fork logic for not-yet-started or already-stopped threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85604
diff
changeset
|
1337 # Dangling thread instances must still have their locks reset, |
|
74dc664ad699
Issue #18808 again: fix the after-fork logic for not-yet-started or already-stopped threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85604
diff
changeset
|
1338 # because someone may join() them. |
|
74dc664ad699
Issue #18808 again: fix the after-fork logic for not-yet-started or already-stopped threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85604
diff
changeset
|
1339 threads = set(_enumerate()) |
|
74dc664ad699
Issue #18808 again: fix the after-fork logic for not-yet-started or already-stopped threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85604
diff
changeset
|
1340 threads.update(_dangling) |
|
74dc664ad699
Issue #18808 again: fix the after-fork logic for not-yet-started or already-stopped threads.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85604
diff
changeset
|
1341 for thread in threads: |
|
74037
de962ec0faaa
Issue #11870: threading: Properly reinitialize threads internal locks and
Charles-François Natali <neologix@free.fr>
parents:
72985
diff
changeset
|
1342 # Any lock/condition variable may be currently locked or in an |
|
de962ec0faaa
Issue #11870: threading: Properly reinitialize threads internal locks and
Charles-François Natali <neologix@free.fr>
parents:
72985
diff
changeset
|
1343 # invalid state, so we reinitialize them. |
| 48516 | 1344 if thread is current: |
|
49058
179f8d89ad19
Issue #874900: fix behaviour of threading module after a fork.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48941
diff
changeset
|
1345 # There is only one active thread. We reset the ident to |
|
179f8d89ad19
Issue #874900: fix behaviour of threading module after a fork.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48941
diff
changeset
|
1346 # its new value since it can have changed. |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
1347 thread._reset_internal_locks(True) |
|
70530
5548197d1f22
Close #12028: Make threading._get_ident() public, rename it to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
69710
diff
changeset
|
1348 ident = get_ident() |
|
49058
179f8d89ad19
Issue #874900: fix behaviour of threading module after a fork.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48941
diff
changeset
|
1349 thread._ident = ident |
| 48516 | 1350 new_active[ident] = thread |
| 1351 else: | |
| 1352 # All the others are already stopped. | |
|
85598
d52b68edbca6
Issue #18808: Thread.join() now waits for the underlying thread state to be destroyed before returning.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85521
diff
changeset
|
1353 thread._reset_internal_locks(False) |
|
74037
de962ec0faaa
Issue #11870: threading: Properly reinitialize threads internal locks and
Charles-François Natali <neologix@free.fr>
parents:
72985
diff
changeset
|
1354 thread._stop() |
| 48516 | 1355 |
| 1356 _limbo.clear() | |
| 1357 _active.clear() | |
| 1358 _active.update(new_active) | |
| 1359 assert len(_active) == 1 |
