Mercurial > cpython-lru_cache-weakref
annotate Lib/threading.py @ 73807:1efefeda00a7 3.2
#8414: add more tests for "assert". Initial patch by Gregory Nofi.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 02 Dec 2011 18:22:52 +0200 |
| parents | e19ccbbe70dd |
| children | de962ec0faaa bfbe144986d7 |
| 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 |
|
7ea7637341d0
- prefer "import ... as" to "import / (assignments) / del" for most things
Fred Drake <fdrake@acm.org>
parents:
26883
diff
changeset
|
6 from time import time as _time, sleep as _sleep |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
29725
diff
changeset
|
7 from traceback import format_exc as _format_exc |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
30862
diff
changeset
|
8 from collections import deque |
|
71361
2bedea96de60
Issue #12573: Add resource checks for dangling Thread and Process objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
9 from _weakrefset import 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
|
10 |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
11 # 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
|
12 # 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
|
13 # the convention of camelCase function and method names from that |
|
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
14 # language. Those originaly names are not in any imminent danger of |
|
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
15 # 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
|
16 # 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
|
17 # 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
|
18 # 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
|
19 # 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
|
20 |
|
67533
2244ac6c2a9b
Add entry for Barrier objects.
Raymond Hettinger <python@rcn.com>
parents:
67354
diff
changeset
|
21 __all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event', |
|
2244ac6c2a9b
Add entry for Barrier objects.
Raymond Hettinger <python@rcn.com>
parents:
67354
diff
changeset
|
22 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier', |
|
2244ac6c2a9b
Add entry for Barrier objects.
Raymond Hettinger <python@rcn.com>
parents:
67354
diff
changeset
|
23 'Timer', '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
|
24 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 # 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
|
26 _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
|
27 _allocate_lock = _thread.allocate_lock |
|
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
28 _get_ident = _thread.get_ident |
|
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
29 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
|
30 try: |
|
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
31 _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
|
32 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
|
33 _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
|
34 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
|
35 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
|
36 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 |
|
29725
7d0af4561eed
Make the classes exposed by threading.py new-style classes. This is
Tim Peters <tim.peters@gmail.com>
parents:
29724
diff
changeset
|
38 # Debug support (adapted from ihooks.py). |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
29724
3b7b4fd5f7ca
Resolved minor XXX question in the obvious way.
Tim Peters <tim.peters@gmail.com>
parents:
29686
diff
changeset
|
40 _VERBOSE = False |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 |
|
29725
7d0af4561eed
Make the classes exposed by threading.py new-style classes. This is
Tim Peters <tim.peters@gmail.com>
parents:
29724
diff
changeset
|
44 class _Verbose(object): |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
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 def __init__(self, verbose=None): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 if verbose is None: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 verbose = _VERBOSE |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
49 self._verbose = verbose |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 def _note(self, format, *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
|
52 if self._verbose: |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 format = format % args |
|
66990
c70aa011c059
Merged revisions 87341 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60379
diff
changeset
|
54 # Issue #4188: calling current_thread() can incur an infinite |
|
c70aa011c059
Merged revisions 87341 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60379
diff
changeset
|
55 # recursion if it has to create a DummyThread on the fly. |
|
c70aa011c059
Merged revisions 87341 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60379
diff
changeset
|
56 ident = _get_ident() |
|
c70aa011c059
Merged revisions 87341 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60379
diff
changeset
|
57 try: |
|
c70aa011c059
Merged revisions 87341 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60379
diff
changeset
|
58 name = _active[ident].name |
|
c70aa011c059
Merged revisions 87341 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60379
diff
changeset
|
59 except KeyError: |
|
c70aa011c059
Merged revisions 87341 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60379
diff
changeset
|
60 name = "<OS thread %d>" % ident |
|
c70aa011c059
Merged revisions 87341 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
60379
diff
changeset
|
61 format = "%s: %s\n" % (name, format) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 _sys.stderr.write(format) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 # Disable this when using "python -O" |
|
29725
7d0af4561eed
Make the classes exposed by threading.py new-style classes. This is
Tim Peters <tim.peters@gmail.com>
parents:
29724
diff
changeset
|
66 class _Verbose(object): |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 def __init__(self, verbose=None): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 def _note(self, *args): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 |
|
29679
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
72 # 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
|
73 |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
74 _profile_hook = None |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
75 _trace_hook = None |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
76 |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
77 def setprofile(func): |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
78 global _profile_hook |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
79 _profile_hook = func |
|
29686
371092ce5c2b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
29683
diff
changeset
|
80 |
|
29679
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
81 def settrace(func): |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
82 global _trace_hook |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
83 _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
|
84 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 # 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
|
86 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 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
|
88 |
|
57172
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
89 def RLock(verbose=None, *args, **kwargs): |
|
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
90 if verbose is None: |
|
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
91 verbose = _VERBOSE |
|
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
92 if (__debug__ and verbose) or _CRLock is None: |
|
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
93 return _PyRLock(verbose, *args, **kwargs) |
|
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
94 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
|
95 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 class _RLock(_Verbose): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15586
diff
changeset
|
97 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 def __init__(self, verbose=None): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 _Verbose.__init__(self, verbose) |
|
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): |
|
57160
fe33562d1f95
Merged revisions 76138,76173 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57121
diff
changeset
|
114 me = _get_ident() |
|
fe33562d1f95
Merged revisions 76138,76173 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57121
diff
changeset
|
115 if self._owner == me: |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
116 self._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
|
117 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 self._note("%s.acquire(%s): recursive success", self, blocking) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 if __debug__: |
|
34512
cb7896d77de5
Fixed typo in verbose output.
Brett Cannon <bcannon@gmail.com>
parents:
34363
diff
changeset
|
125 self._note("%s.acquire(%s): initial success", self, blocking) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 self._note("%s.acquire(%s): failure", self, blocking) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 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
|
130 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
131 __enter__ = acquire |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
132 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 def release(self): |
|
57160
fe33562d1f95
Merged revisions 76138,76173 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57121
diff
changeset
|
134 if self._owner != _get_ident(): |
|
56894
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56002
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 self._note("%s.release(): final release", self) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 self._note("%s.release(): non-final release", self) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
146 def __exit__(self, t, v, tb): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
147 self.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
148 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 # 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
|
150 |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
151 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
|
152 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
|
153 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
|
154 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 self._note("%s._acquire_restore()", self) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 def _release_save(self): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 self._note("%s._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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 def _is_owned(self): |
|
57160
fe33562d1f95
Merged revisions 76138,76173 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57121
diff
changeset
|
168 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
|
169 |
|
57172
6d91aaadddd0
Issue #3001: Add a C implementation of recursive locks which is used by
Antoine Pitrou <solipsis@pitrou.net>
parents:
57160
diff
changeset
|
170 _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
|
171 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 def Condition(*args, **kwargs): |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26905
diff
changeset
|
174 return _Condition(*args, **kwargs) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 class _Condition(_Verbose): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 def __init__(self, lock=None, verbose=None): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 _Verbose.__init__(self, verbose) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 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
|
181 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
|
182 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
|
183 # 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
|
184 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
|
185 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
|
186 # 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
|
187 # 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
|
188 # 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
|
189 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 self._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
|
191 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
|
192 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 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
|
195 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
|
196 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 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
|
199 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
|
200 pass |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
201 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
|
202 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
203 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
|
204 return self._lock.__enter__() |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
205 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
206 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
|
207 return self._lock.__exit__(*args) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
208 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 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
|
210 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
|
211 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 def _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
|
213 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
|
214 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 def _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
|
216 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
|
217 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 def _is_owned(self): |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
219 # 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
|
220 # 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
|
221 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
|
222 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
|
223 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
|
224 else: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
21981
diff
changeset
|
225 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
|
226 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 def wait(self, timeout=None): |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
228 if not self._is_owned(): |
|
56894
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56002
diff
changeset
|
229 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
|
230 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
|
231 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
|
232 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
|
233 saved_state = self._release_save() |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
234 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
|
235 if timeout is None: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
236 waiter.acquire() |
|
65675
6dff581d30f3
#10218: return timeout status from Condition.wait, mirroring other primitives' behavior.
Georg Brandl <georg@python.org>
parents:
63166
diff
changeset
|
237 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
|
238 if __debug__: |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
239 self._note("%s.wait(): got it", self) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 else: |
|
60598
01d1fd775d16
Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents:
60377
diff
changeset
|
241 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
|
242 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
|
243 else: |
|
01d1fd775d16
Issue #7316: the acquire() method of lock objects in the :mod:`threading`
Antoine Pitrou <solipsis@pitrou.net>
parents:
60377
diff
changeset
|
244 gotit = waiter.acquire(False) |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
245 if not gotit: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
246 if __debug__: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
247 self._note("%s.wait(%s): timed out", self, timeout) |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
248 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
|
249 self._waiters.remove(waiter) |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
250 except ValueError: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
251 pass |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
252 else: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
253 if __debug__: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
254 self._note("%s.wait(%s): got it", self, timeout) |
|
65675
6dff581d30f3
#10218: return timeout status from Condition.wait, mirroring other primitives' behavior.
Georg Brandl <georg@python.org>
parents:
63166
diff
changeset
|
255 return gotit |
|
17303
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
256 finally: |
|
836cdf8d1245
SF bug [#410708] Condition.wait() and KeyboardInterrupt.
Tim Peters <tim.peters@gmail.com>
parents:
15873
diff
changeset
|
257 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
|
258 |
|
66229
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
259 def wait_for(self, predicate, timeout=None): |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
260 endtime = None |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
261 waittime = timeout |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
262 result = predicate() |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
263 while not result: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
264 if waittime is not None: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
265 if endtime is None: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
266 endtime = _time() + waittime |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
267 else: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
268 waittime = endtime - _time() |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
269 if waittime <= 0: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
270 if __debug__: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
271 self._note("%s.wait_for(%r, %r): Timed out.", |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
272 self, predicate, timeout) |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
273 break |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
274 if __debug__: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
275 self._note("%s.wait_for(%r, %r): Waiting with timeout=%s.", |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
276 self, predicate, timeout, waittime) |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
277 self.wait(waittime) |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
278 result = predicate() |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
279 else: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
280 if __debug__: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
281 self._note("%s.wait_for(%r, %r): Success.", |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
282 self, predicate, timeout) |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
283 return result |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
284 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 def notify(self, n=1): |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
286 if not self._is_owned(): |
|
56894
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56002
diff
changeset
|
287 raise RuntimeError("cannot notify on 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
|
288 __waiters = 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
|
289 waiters = __waiters[:n] |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 if not waiters: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 self._note("%s.notify(): no waiters", self) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 return |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 self._note("%s.notify(): notifying %d waiter%s", self, n, |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 n!=1 and "s" or "") |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 for waiter in waiters: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 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
|
298 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 __waiters.remove(waiter) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 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
|
301 pass |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
303 def notify_all(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
|
304 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
|
305 |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
306 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
|
307 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 def Semaphore(*args, **kwargs): |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26905
diff
changeset
|
310 return _Semaphore(*args, **kwargs) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 class _Semaphore(_Verbose): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 |
| 11876 | 314 # 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
|
315 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 def __init__(self, value=1, verbose=None): |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
317 if value < 0: |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
318 raise ValueError("semaphore initial value must be >= 0") |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 _Verbose.__init__(self, verbose) |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
320 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
|
321 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
|
322 |
|
60665
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
323 def acquire(self, blocking=True, timeout=None): |
|
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
324 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
|
325 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
|
326 rc = False |
|
60665
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
327 endtime = None |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
328 self._cond.acquire() |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
329 while self._value == 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
|
330 if not blocking: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 break |
|
19377
6386f01f2401
add debug calls to self._note for the Semaphore class. This closes bug
Skip Montanaro <skip@pobox.com>
parents:
19210
diff
changeset
|
332 if __debug__: |
|
6386f01f2401
add debug calls to self._note for the Semaphore class. This closes bug
Skip Montanaro <skip@pobox.com>
parents:
19210
diff
changeset
|
333 self._note("%s.acquire(%s): blocked waiting, value=%s", |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
334 self, blocking, self._value) |
|
60665
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
335 if 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
|
336 if endtime is None: |
|
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
337 endtime = _time() + timeout |
|
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
338 else: |
|
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
339 timeout = endtime - _time() |
|
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
340 if timeout <= 0: |
|
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
341 break |
|
bac99229da2d
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
Antoine Pitrou <solipsis@pitrou.net>
parents:
60598
diff
changeset
|
342 self._cond.wait(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
|
343 else: |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
344 self._value = self._value - 1 |
|
19377
6386f01f2401
add debug calls to self._note for the Semaphore class. This closes bug
Skip Montanaro <skip@pobox.com>
parents:
19210
diff
changeset
|
345 if __debug__: |
|
19379
2a083e050617
of course I muffed it separating the notes code from the initial_value
Skip Montanaro <skip@pobox.com>
parents:
19377
diff
changeset
|
346 self._note("%s.acquire: success, value=%s", |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
347 self, self._value) |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
348 rc = 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
|
349 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
|
350 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
|
351 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
352 __enter__ = acquire |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
353 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 def release(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
|
355 self._cond.acquire() |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
356 self._value = self._value + 1 |
|
19377
6386f01f2401
add debug calls to self._note for the Semaphore class. This closes bug
Skip Montanaro <skip@pobox.com>
parents:
19210
diff
changeset
|
357 if __debug__: |
|
19379
2a083e050617
of course I muffed it separating the notes code from the initial_value
Skip Montanaro <skip@pobox.com>
parents:
19377
diff
changeset
|
358 self._note("%s.release: success, value=%s", |
|
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, self._value) |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
360 self._cond.notify() |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
361 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
|
362 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
363 def __exit__(self, t, v, tb): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
364 self.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
35932
diff
changeset
|
365 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 |
|
19404
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
367 def BoundedSemaphore(*args, **kwargs): |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26905
diff
changeset
|
368 return _BoundedSemaphore(*args, **kwargs) |
|
19404
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
369 |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
370 class _BoundedSemaphore(_Semaphore): |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
371 """Semaphore that checks that # releases is <= # acquires""" |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
372 def __init__(self, value=1, verbose=None): |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
373 _Semaphore.__init__(self, value, verbose) |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
374 self._initial_value = value |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
375 |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
376 def release(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
|
377 if self._value >= self._initial_value: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42679
diff
changeset
|
378 raise ValueError("Semaphore released too many times") |
|
19404
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
379 return _Semaphore.release(self) |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
380 |
|
6125d9f92441
Added new BoundedSemaphore class. Closes bug 452836.
Skip Montanaro <skip@pobox.com>
parents:
19379
diff
changeset
|
381 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 def Event(*args, **kwargs): |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
26905
diff
changeset
|
383 return _Event(*args, **kwargs) |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
384 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 class _Event(_Verbose): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 # 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
|
388 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
389 def __init__(self, verbose=None): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
390 _Verbose.__init__(self, verbose) |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
391 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
|
392 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
|
393 |
|
67355
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
394 def _reset_internal_locks(self): |
|
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
395 # 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
|
396 self._cond.__init__() |
|
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
397 |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
398 def is_set(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
|
399 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
|
400 |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
401 isSet = is_set |
|
48757
54138ee20e57
add full deprecation warnings for old threading APIs
Benjamin Peterson <benjamin@python.org>
parents:
48748
diff
changeset
|
402 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
403 def set(self): |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
404 self._cond.acquire() |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
405 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
|
406 self._flag = True |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
407 self._cond.notify_all() |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
408 finally: |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
409 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
|
410 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 def clear(self): |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
412 self._cond.acquire() |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
413 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
|
414 self._flag = False |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
415 finally: |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
416 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
|
417 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
418 def wait(self, timeout=None): |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
419 self._cond.acquire() |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
420 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
|
421 if not self._flag: |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
422 self._cond.wait(timeout) |
|
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
|
423 return self._flag |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
424 finally: |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
425 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
|
426 |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
427 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
428 # 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
|
429 # the CyclicBarrier class from Java. See |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
430 # 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
|
431 # 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
|
432 # CyclicBarrier.html |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
433 # for information. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
434 # 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
|
435 # 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
|
436 # 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
|
437 # similar to 'draining' except that threads leave with a BrokenBarrierError, |
|
72985
e19ccbbe70dd
Remove duplication.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
71361
diff
changeset
|
438 # and a 'broken' state in which all threads get the exception. |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
439 class Barrier(_Verbose): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
440 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
441 Barrier. Useful for synchronizing a fixed number of threads |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
442 at known synchronization points. Threads block on 'wait()' and are |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
443 simultaneously once they have all made that call. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
444 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
445 def __init__(self, parties, action=None, timeout=None, verbose=None): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
446 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
447 Create a barrier, initialised to 'parties' threads. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
448 'action' is a callable which, when supplied, will be called |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
449 by one of the threads after they have all entered the |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
450 barrier and just prior to releasing them all. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
451 If a 'timeout' is provided, it is uses as the default for |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
452 all subsequent 'wait()' calls. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
453 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
454 _Verbose.__init__(self, verbose) |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
455 self._cond = Condition(Lock()) |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
456 self._action = action |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
457 self._timeout = timeout |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
458 self._parties = parties |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
459 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
|
460 self._count = 0 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
461 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
462 def wait(self, timeout=None): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
463 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
464 Wait for the barrier. When the specified number of threads have |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
465 started waiting, they are all simultaneously awoken. If an 'action' |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
466 was provided for the barrier, one of the threads will have executed |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
467 that callback prior to returning. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
468 Returns an individual index number from 0 to 'parties-1'. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
469 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
470 if timeout is None: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
471 timeout = self._timeout |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
472 with self._cond: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
473 self._enter() # Block while the barrier drains. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
474 index = self._count |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
475 self._count += 1 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
476 try: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
477 if index + 1 == self._parties: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
478 # We release the barrier |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
479 self._release() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
480 else: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
481 # We wait until someone releases us |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
482 self._wait(timeout) |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
483 return index |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
484 finally: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
485 self._count -= 1 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
486 # Wake up any threads waiting for barrier to drain. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
487 self._exit() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
488 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
489 # 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
|
490 # if it is broken. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
491 def _enter(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
492 while self._state in (-1, 1): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
493 # It is draining or resetting, wait until done |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
494 self._cond.wait() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
495 #see if the barrier is in a broken state |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
496 if self._state < 0: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
497 raise BrokenBarrierError |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
498 assert self._state == 0 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
499 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
500 # Optionally run the 'action' and release the threads waiting |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
501 # in the barrier. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
502 def _release(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
503 try: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
504 if self._action: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
505 self._action() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
506 # enter draining state |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
507 self._state = 1 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
508 self._cond.notify_all() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
509 except: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
510 #an exception during the _action handler. Break and reraise |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
511 self._break() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
512 raise |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
513 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
514 # 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
|
515 # if the barrier is reset or broken. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
516 def _wait(self, timeout): |
|
66229
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
517 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
|
518 #timed out. Break the barrier |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
519 self._break() |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
520 raise BrokenBarrierError |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
521 if self._state < 0: |
|
483bbebc57bf
Issue 10260
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65677
diff
changeset
|
522 raise BrokenBarrierError |
|
65677
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
523 assert self._state == 1 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
524 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
525 # 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
|
526 # waiting for the barrier to drain. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
527 def _exit(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
528 if self._count == 0: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
529 if self._state in (-1, 1): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
530 #resetting or draining |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
531 self._state = 0 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
532 self._cond.notify_all() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
533 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
534 def reset(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
535 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
536 Reset the barrier to the initial state. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
537 Any threads currently waiting will get the BrokenBarrier exception |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
538 raised. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
539 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
540 with self._cond: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
541 if self._count > 0: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
542 if self._state == 0: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
543 #reset the barrier, waking up threads |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
544 self._state = -1 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
545 elif self._state == -2: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
546 #was broken, set it to reset state |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
547 #which clears when the last thread exits |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
548 self._state = -1 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
549 else: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
550 self._state = 0 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
551 self._cond.notify_all() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
552 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
553 def abort(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
554 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
555 Place the barrier into a 'broken' state. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
556 Useful in case of error. Any currently waiting threads and |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
557 threads attempting to 'wait()' will have BrokenBarrierError |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
558 raised. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
559 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
560 with self._cond: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
561 self._break() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
562 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
563 def _break(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
564 # 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
|
565 # a broken state all parties awakened. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
566 self._state = -2 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
567 self._cond.notify_all() |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
568 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
569 @property |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
570 def parties(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
571 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
572 Return the number of threads required to trip the barrier. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
573 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
574 return self._parties |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
575 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
576 @property |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
577 def n_waiting(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
578 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
579 Return the number of threads that are currently waiting at the barrier. |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
580 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
581 # 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
|
582 # 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
|
583 if self._state == 0: |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
584 return self._count |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
585 return 0 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
586 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
587 @property |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
588 def broken(self): |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
589 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
590 Return True if the barrier is in a broken state |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
591 """ |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
592 return self._state == -2 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
593 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
594 #exception raised by the Barrier class |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
595 class BrokenBarrierError(RuntimeError): pass |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
596 |
|
5a882f789ec1
issue 8777
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
65675
diff
changeset
|
597 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
598 # 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
|
599 _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
|
600 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
|
601 global _counter |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
602 _counter = _counter + 1 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
603 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
|
604 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
605 # 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
|
606 _active_limbo_lock = _allocate_lock() |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
607 _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
|
608 _limbo = {} |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
609 |
|
71361
2bedea96de60
Issue #12573: Add resource checks for dangling Thread and Process objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
610 # For debug and leak testing |
|
2bedea96de60
Issue #12573: Add resource checks for dangling Thread and Process objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
611 _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
|
612 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
613 # 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
|
614 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
615 class Thread(_Verbose): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
616 |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
617 __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
|
618 # 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
|
619 # 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
|
620 # 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
|
621 # 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
|
622 __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
|
623 # 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
|
624 # 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
|
625 #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
|
626 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
627 def __init__(self, group=None, target=None, name=None, |
|
35236
d0c6965b525e
bug [ 1238170 ] threading.Thread uses {} as default argument
Georg Brandl <georg@python.org>
parents:
34512
diff
changeset
|
628 args=(), kwargs=None, verbose=None): |
|
8474
049aa3a8a172
Get rid of tabnanny's last complaints.
Guido van Rossum <guido@python.org>
parents:
8454
diff
changeset
|
629 assert group is None, "group argument must be None for now" |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
630 _Verbose.__init__(self, verbose) |
|
35236
d0c6965b525e
bug [ 1238170 ] threading.Thread uses {} as default argument
Georg Brandl <georg@python.org>
parents:
34512
diff
changeset
|
631 if kwargs is None: |
|
d0c6965b525e
bug [ 1238170 ] threading.Thread uses {} as default argument
Georg Brandl <georg@python.org>
parents:
34512
diff
changeset
|
632 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
|
633 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
|
634 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
|
635 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
|
636 self._kwargs = kwargs |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
637 self._daemonic = self._set_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
|
638 self._ident = 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
|
639 self._started = Event() |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
640 self._stopped = False |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
641 self._block = 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
|
642 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
|
643 # 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
|
644 # 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
|
645 self._stderr = _sys.stderr |
|
71361
2bedea96de60
Issue #12573: Add resource checks for dangling Thread and Process objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
646 _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
|
647 |
|
67355
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
648 def _reset_internal_locks(self): |
|
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
649 # 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
|
650 # 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
|
651 if hasattr(self, '_block'): # DummyThread deletes _block |
|
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
652 self._block.__init__() |
|
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
653 self._started._reset_internal_locks() |
|
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
654 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
655 def _set_daemon(self): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
656 # Overridden in _MainThread and _DummyThread |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
657 return current_thread().daemon |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
658 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
659 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
|
660 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
|
661 status = "initial" |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
662 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
|
663 status = "started" |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
664 if self._stopped: |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
665 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
|
666 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
|
667 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
|
668 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
|
669 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
|
670 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
|
671 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
672 def start(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
|
673 if not self._initialized: |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
674 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
|
675 |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
676 if self._started.is_set(): |
|
60379
c10024abd118
Merged revisions 79819 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
59203
diff
changeset
|
677 raise RuntimeError("threads can only be started once") |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
678 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
679 self._note("%s.start(): starting thread", self) |
|
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
|
680 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
|
681 _limbo[self] = self |
|
59203
9e8113e76adb
Merged revisions 78519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
57160
diff
changeset
|
682 try: |
|
9e8113e76adb
Merged revisions 78519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
57160
diff
changeset
|
683 _start_new_thread(self._bootstrap, ()) |
|
9e8113e76adb
Merged revisions 78519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
57160
diff
changeset
|
684 except Exception: |
|
9e8113e76adb
Merged revisions 78519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
57160
diff
changeset
|
685 with _active_limbo_lock: |
|
9e8113e76adb
Merged revisions 78519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
57160
diff
changeset
|
686 del _limbo[self] |
|
9e8113e76adb
Merged revisions 78519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
57160
diff
changeset
|
687 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
|
688 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
|
689 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
690 def run(self): |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44943
diff
changeset
|
691 try: |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44943
diff
changeset
|
692 if self._target: |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44943
diff
changeset
|
693 self._target(*self._args, **self._kwargs) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44943
diff
changeset
|
694 finally: |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44943
diff
changeset
|
695 # 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
|
696 # 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
|
697 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
|
698 |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
699 def _bootstrap(self): |
|
42678
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
700 # 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
|
701 # exceptions during interpreter cleanup. Those typically |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
702 # 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
|
703 # 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
|
704 # 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
|
705 # _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
|
706 # 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
|
707 # 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
|
708 # 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
|
709 # _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
|
710 # 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
|
711 # 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
|
712 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
|
713 self._bootstrap_inner() |
|
42678
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
714 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
|
715 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
|
716 return |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
717 raise |
|
af6a7190cabc
Merged revisions 57152-57220 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
718 |
|
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
|
719 def _set_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
|
720 self._ident = _get_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
|
721 |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
722 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
|
723 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
|
724 self._set_ident() |
|
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
|
725 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
|
726 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
|
727 _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
|
728 del _limbo[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
|
729 if __debug__: |
|
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
|
730 self._note("%s._bootstrap(): thread started", self) |
|
29679
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
731 |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
732 if _trace_hook: |
|
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
|
733 self._note("%s._bootstrap(): registering trace hook", self) |
|
29679
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
734 _sys.settrace(_trace_hook) |
|
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
735 if _profile_hook: |
|
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
|
736 self._note("%s._bootstrap(): registering profile hook", self) |
|
29679
d0f15977f37f
Add settrace() and setprofile() functions to the threading library.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29676
diff
changeset
|
737 _sys.setprofile(_profile_hook) |
|
29686
371092ce5c2b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
29683
diff
changeset
|
738 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
739 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
740 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
|
741 except SystemExit: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
742 if __debug__: |
|
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
|
743 self._note("%s._bootstrap(): raised SystemExit", 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
|
744 except: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
745 if __debug__: |
|
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
|
746 self._note("%s._bootstrap(): unhandled exception", self) |
|
32475
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
747 # 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
|
748 # 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
|
749 # _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
|
750 # self. |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
751 if _sys: |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
752 _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
|
753 (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
|
754 else: |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
755 # 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
|
756 # 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
|
757 # 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
|
758 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
|
759 try: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
760 print(( |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
761 "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
|
762 " (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
|
763 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
|
764 "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
|
765 while exc_tb: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
766 print(( |
|
32475
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
767 ' 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
|
768 (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
|
769 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
|
770 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
|
771 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
|
772 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
|
773 # 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
|
774 # 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
|
775 finally: |
|
796ccb9082ef
threading.Thread objects will now print a traceback for an exception raised
Brett Cannon <bcannon@gmail.com>
parents:
31629
diff
changeset
|
776 del exc_type, exc_value, exc_tb |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
777 else: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
778 if __debug__: |
|
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
|
779 self._note("%s._bootstrap(): normal return", self) |
|
46161
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45549
diff
changeset
|
780 finally: |
|
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45549
diff
changeset
|
781 # 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
|
782 # 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
|
783 # 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
|
784 # 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
|
785 #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
|
786 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
|
787 finally: |
|
44885
fefaa0e66fdd
Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
788 with _active_limbo_lock: |
|
fefaa0e66fdd
Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
789 self._stop() |
|
fefaa0e66fdd
Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
790 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
|
791 # 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
|
792 # grabs _active_limbo_lock. |
|
fefaa0e66fdd
Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
793 del _active[_get_ident()] |
|
fefaa0e66fdd
Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
794 except: |
|
fefaa0e66fdd
Merged revisions 60176-60209 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
795 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
|
796 |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
797 def _stop(self): |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
798 self._block.acquire() |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
799 self._stopped = True |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
800 self._block.notify_all() |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
801 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
|
802 |
|
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 def _delete(self): |
|
32778
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
804 "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
|
805 |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
806 # 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
|
807 # |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
808 # 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
|
809 # 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
|
810 # 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
|
811 # 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
|
812 # 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
|
813 # 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
|
814 # |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
815 # 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
|
816 # 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
|
817 # 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
|
818 # 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
|
819 # '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
|
820 # 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
|
821 # |
|
7af695bc5480
Thread.__delete: Discussion of internal obscurities belongs in comments
Tim Peters <tim.peters@gmail.com>
parents:
32774
diff
changeset
|
822 # 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
|
823 # _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
|
824 # 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
|
825 # 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
|
826 |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
827 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
|
828 with _active_limbo_lock: |
|
32774
8a91cd7d7e82
Fix bug where a KeyError was raised if -O was being used for the interpreter
Brett Cannon <bcannon@gmail.com>
parents:
32673
diff
changeset
|
829 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
|
830 # 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
|
831 # 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
|
832 # 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
|
833 # 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
|
834 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
|
835 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
|
836 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
|
837 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
838 def join(self, timeout=None): |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
839 if not self._initialized: |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
840 raise RuntimeError("Thread.__init__() not called") |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
841 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
|
842 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
|
843 if self is current_thread(): |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
844 raise RuntimeError("cannot join current thread") |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
845 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
846 if __debug__: |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
847 if not self._stopped: |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
848 self._note("%s.join(): waiting until thread stops", self) |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
849 |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
850 self._block.acquire() |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
851 try: |
|
35932
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
852 if timeout is None: |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
853 while not self._stopped: |
|
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
854 self._block.wait() |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
855 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
856 self._note("%s.join(): thread stopped", self) |
|
35932
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
857 else: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
858 deadline = _time() + timeout |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
859 while not self._stopped: |
|
35932
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
860 delay = deadline - _time() |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
861 if delay <= 0: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
862 if __debug__: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
863 self._note("%s.join(): timed out", self) |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
864 break |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
865 self._block.wait(delay) |
|
35932
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
866 else: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
867 if __debug__: |
|
b7c6c461a808
Prevent threading.Thread.join() from blocking when a previous call raised an
Brett Cannon <bcannon@gmail.com>
parents:
35236
diff
changeset
|
868 self._note("%s.join(): thread stopped", self) |
|
44943
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
869 finally: |
|
6547be11f443
Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44885
diff
changeset
|
870 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
|
871 |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
872 @property |
|
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
873 def name(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
|
874 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
|
875 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
|
876 |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
877 @name.setter |
|
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
878 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
|
879 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
|
880 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
|
881 |
|
48743
76620010d782
Merged revisions 65818 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48516
diff
changeset
|
882 @property |
|
76620010d782
Merged revisions 65818 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48516
diff
changeset
|
883 def ident(self): |
|
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
|
884 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
|
885 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
|
886 |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
887 def is_alive(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
|
888 assert self._initialized, "Thread.__init__() not called" |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
889 return self._started.is_set() and not self._stopped |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15586
diff
changeset
|
890 |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
891 isAlive = is_alive |
|
48757
54138ee20e57
add full deprecation warnings for old threading APIs
Benjamin Peterson <benjamin@python.org>
parents:
48748
diff
changeset
|
892 |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
893 @property |
|
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
894 def daemon(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
|
895 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
|
896 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
|
897 |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
898 @daemon.setter |
|
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
899 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
|
900 if not self._initialized: |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
901 raise RuntimeError("Thread.__init__() not called") |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
902 if self._started.is_set(): |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
903 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
|
904 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
|
905 |
|
48748
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
906 def isDaemon(self): |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
907 return self.daemon |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
908 |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
909 def setDaemon(self, daemonic): |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
910 self.daemon = daemonic |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
911 |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
912 def getName(self): |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
913 return self.name |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
914 |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
915 def setName(self, name): |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
916 self.name = name |
|
d6537a3e1b73
Merged revisions 65826 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48745
diff
changeset
|
917 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
918 # 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
|
919 |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
920 def Timer(*args, **kwargs): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
921 return _Timer(*args, **kwargs) |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
922 |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
923 class _Timer(Thread): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
924 """Call a function after a specified number of seconds: |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
925 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
926 t = Timer(30.0, f, args=[], kwargs={}) |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
927 t.start() |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
928 t.cancel() # stop the timer's action if it's still waiting |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
929 """ |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
930 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
931 def __init__(self, interval, function, args=[], kwargs={}): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
932 Thread.__init__(self) |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
933 self.interval = interval |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
934 self.function = function |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
935 self.args = args |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
936 self.kwargs = kwargs |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
937 self.finished = Event() |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
938 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
939 def cancel(self): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
940 """Stop the timer if it hasn't finished yet""" |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
941 self.finished.set() |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19727
diff
changeset
|
942 |
|
19727
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
943 def run(self): |
|
f3036d4e3c22
Patch #428326: New class threading.Timer.
Martin v. Löwis <martin@v.loewis.de>
parents:
19404
diff
changeset
|
944 self.finished.wait(self.interval) |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
945 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
|
946 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
|
947 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
|
948 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
949 # 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
|
950 # 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
|
951 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
952 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
|
953 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
954 def __init__(self): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
955 Thread.__init__(self, name="MainThread") |
|
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
|
956 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
|
957 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
|
958 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
|
959 _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
|
960 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
961 def _set_daemon(self): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
962 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
|
963 |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
964 def _exitfunc(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
|
965 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
|
966 t = _pickSomeNonDaemonThread() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
967 if t: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
968 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
969 self._note("%s: waiting for other threads", self) |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
970 while t: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
971 t.join() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
972 t = _pickSomeNonDaemonThread() |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
973 if __debug__: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
974 self._note("%s: exiting", 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
|
975 self._delete() |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
976 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
977 def _pickSomeNonDaemonThread(): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
978 for t in enumerate(): |
|
48745
2614d501de9a
change is_daemon, set_daemon, get_name, and set_name to properties
Benjamin Peterson <benjamin@python.org>
parents:
48743
diff
changeset
|
979 if not t.daemon and t.is_alive(): |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
980 return t |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
981 return None |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
982 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
983 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
984 # 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
|
985 # 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
|
986 # 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
|
987 # 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
|
988 # 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
|
989 # 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
|
990 # 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
|
991 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
992 class _DummyThread(Thread): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15586
diff
changeset
|
993 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
994 def __init__(self): |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
995 Thread.__init__(self, name=_newname("Dummy-%d")) |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
996 |
|
67355
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
997 # Thread._block consumes an OS-level locking primitive, which |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
998 # can never be used by a _DummyThread. Since a _DummyThread |
|
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
999 # instance is immortal, that's bad, so release this resource. |
|
42679
e71c3223810f
Get rid of all __private variables and methods in the threading module.
Guido van Rossum <guido@python.org>
parents:
42678
diff
changeset
|
1000 del self._block |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34361
diff
changeset
|
1001 |
|
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
|
1002 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
|
1003 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
|
1004 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
|
1005 _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
|
1006 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1007 def _set_daemon(self): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
1008 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
|
1009 |
|
21981
4e9cc26e03fd
SF #515023. Make _DummyThread.join() signature match base class (Thread)
Neal Norwitz <nnorwitz@gmail.com>
parents:
21589
diff
changeset
|
1010 def join(self, timeout=None): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
1011 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
|
1012 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1013 |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1014 # 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
|
1015 |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1016 def 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
|
1017 try: |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1018 return _active[_get_ident()] |
|
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1019 except KeyError: |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1020 ##print "current_thread(): no current thread for", _get_ident() |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1021 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
|
1022 |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
1023 currentThread = current_thread |
|
48757
54138ee20e57
add full deprecation warnings for old threading APIs
Benjamin Peterson <benjamin@python.org>
parents:
48748
diff
changeset
|
1024 |
|
47615
045f83a1b864
Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47555
diff
changeset
|
1025 def active_count(): |
|
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
|
1026 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
|
1027 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
|
1028 |
|
48941
4fc480be1e4d
remove the deprecation warnings for the old threading API; update the docs
Benjamin Peterson <benjamin@python.org>
parents:
48757
diff
changeset
|
1029 activeCount = active_count |
|
48757
54138ee20e57
add full deprecation warnings for old threading APIs
Benjamin Peterson <benjamin@python.org>
parents:
48748
diff
changeset
|
1030 |
|
57121
4ea0a80ed76a
Merged revisions 76119 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56894
diff
changeset
|
1031 def _enumerate(): |
|
4ea0a80ed76a
Merged revisions 76119 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56894
diff
changeset
|
1032 # 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
|
1033 return list(_active.values()) + list(_limbo.values()) |
|
4ea0a80ed76a
Merged revisions 76119 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56894
diff
changeset
|
1034 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1035 def enumerate(): |
|
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
|
1036 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
|
1037 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
|
1038 |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
1039 from _thread import stack_size |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1040 |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1041 # Create the main thread object, |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1042 # and make it available for the interpreter |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1043 # (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
|
1044 |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1045 _shutdown = _MainThread()._exitfunc |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1046 |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
1047 # 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
|
1048 # 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
|
1049 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
1050 try: |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
46304
diff
changeset
|
1051 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
|
1052 except ImportError: |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
1053 from _threading_local import local |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
32475
diff
changeset
|
1054 |
|
7949
58d6815af002
New Java-style threading module. The doc strings are in a separate module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1055 |
| 48516 | 1056 def _after_fork(): |
| 1057 # This function is called by Python/ceval.c:PyEval_ReInitThreads which | |
| 1058 # is called from PyOS_AfterFork. Here we cleanup threading module state | |
| 1059 # that should not exist after a fork. | |
| 1060 | |
| 1061 # Reset _active_limbo_lock, in case we forked while the lock was held | |
| 1062 # by another (non-forked) thread. http://bugs.python.org/issue874900 | |
| 1063 global _active_limbo_lock | |
| 1064 _active_limbo_lock = _allocate_lock() | |
| 1065 | |
| 1066 # fork() only copied the current thread; clear references to others. | |
| 1067 new_active = {} | |
| 1068 current = current_thread() | |
| 1069 with _active_limbo_lock: | |
|
49058
179f8d89ad19
Issue #874900: fix behaviour of threading module after a fork.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48941
diff
changeset
|
1070 for thread in _active.values(): |
| 48516 | 1071 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
|
1072 # 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
|
1073 # its new value since it can have changed. |
|
179f8d89ad19
Issue #874900: fix behaviour of threading module after a fork.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48941
diff
changeset
|
1074 ident = _get_ident() |
|
179f8d89ad19
Issue #874900: fix behaviour of threading module after a fork.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48941
diff
changeset
|
1075 thread._ident = ident |
|
67340
ad2f34380aac
Merged revisions 87710 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
66990
diff
changeset
|
1076 # Any condition variables hanging off of the active thread may |
|
ad2f34380aac
Merged revisions 87710 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
66990
diff
changeset
|
1077 # be in an invalid state, so we reinitialize them. |
|
67355
a3b7c290754a
Merged revisions 87740 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
67340
diff
changeset
|
1078 thread._reset_internal_locks() |
| 48516 | 1079 new_active[ident] = thread |
| 1080 else: | |
| 1081 # All the others are already stopped. | |
| 1082 # We don't call _Thread__stop() because it tries to acquire | |
| 1083 # thread._Thread__block which could also have been held while | |
| 1084 # we forked. | |
|
49058
179f8d89ad19
Issue #874900: fix behaviour of threading module after a fork.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48941
diff
changeset
|
1085 thread._stopped = True |
| 48516 | 1086 |
| 1087 _limbo.clear() | |
| 1088 _active.clear() | |
| 1089 _active.update(new_active) | |
| 1090 assert len(_active) == 1 |
