mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
Remove indirection in threading (issue #10968).
The public names (Thread, Condition, etc.) used to be factory functions returning instances of hidden classes (_Thread, _Condition, etc.), because (if Guido recalls correctly) this code pre-dates the ability to subclass extension types. It is now possible to inherit from Thread and other classes, without having to import the private underscored names like multiprocessing did. A doc update will follow: a patch is under discussion on the issue.
This commit is contained in:
parent
9bce311ea4
commit
0cdd4454f3
3 changed files with 15 additions and 34 deletions
|
@ -51,7 +51,7 @@ import itertools
|
||||||
from multiprocessing import TimeoutError, cpu_count
|
from multiprocessing import TimeoutError, cpu_count
|
||||||
from multiprocessing.dummy.connection import Pipe
|
from multiprocessing.dummy.connection import Pipe
|
||||||
from threading import Lock, RLock, Semaphore, BoundedSemaphore
|
from threading import Lock, RLock, Semaphore, BoundedSemaphore
|
||||||
from threading import Event
|
from threading import Event, Condition
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -84,17 +84,6 @@ class DummyProcess(threading.Thread):
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
|
||||||
class Condition(threading._Condition):
|
|
||||||
# XXX
|
|
||||||
if sys.version_info < (3, 0):
|
|
||||||
notify_all = threading._Condition.notify_all.__func__
|
|
||||||
else:
|
|
||||||
notify_all = threading._Condition.notify_all
|
|
||||||
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#
|
|
||||||
|
|
||||||
Process = DummyProcess
|
Process = DummyProcess
|
||||||
current_process = threading.current_thread
|
current_process = threading.current_thread
|
||||||
current_process()._children = weakref.WeakKeyDictionary()
|
current_process()._children = weakref.WeakKeyDictionary()
|
||||||
|
|
|
@ -172,10 +172,7 @@ class _RLock(_Verbose):
|
||||||
_PyRLock = _RLock
|
_PyRLock = _RLock
|
||||||
|
|
||||||
|
|
||||||
def Condition(*args, **kwargs):
|
class Condition(_Verbose):
|
||||||
return _Condition(*args, **kwargs)
|
|
||||||
|
|
||||||
class _Condition(_Verbose):
|
|
||||||
|
|
||||||
def __init__(self, lock=None, verbose=None):
|
def __init__(self, lock=None, verbose=None):
|
||||||
_Verbose.__init__(self, verbose)
|
_Verbose.__init__(self, verbose)
|
||||||
|
@ -308,10 +305,7 @@ class _Condition(_Verbose):
|
||||||
notifyAll = notify_all
|
notifyAll = notify_all
|
||||||
|
|
||||||
|
|
||||||
def Semaphore(*args, **kwargs):
|
class Semaphore(_Verbose):
|
||||||
return _Semaphore(*args, **kwargs)
|
|
||||||
|
|
||||||
class _Semaphore(_Verbose):
|
|
||||||
|
|
||||||
# After Tim Peters' semaphore class, but not quite the same (no maximum)
|
# After Tim Peters' semaphore class, but not quite the same (no maximum)
|
||||||
|
|
||||||
|
@ -366,25 +360,19 @@ class _Semaphore(_Verbose):
|
||||||
self.release()
|
self.release()
|
||||||
|
|
||||||
|
|
||||||
def BoundedSemaphore(*args, **kwargs):
|
class BoundedSemaphore(Semaphore):
|
||||||
return _BoundedSemaphore(*args, **kwargs)
|
|
||||||
|
|
||||||
class _BoundedSemaphore(_Semaphore):
|
|
||||||
"""Semaphore that checks that # releases is <= # acquires"""
|
"""Semaphore that checks that # releases is <= # acquires"""
|
||||||
def __init__(self, value=1, verbose=None):
|
def __init__(self, value=1, verbose=None):
|
||||||
_Semaphore.__init__(self, value, verbose)
|
Semaphore.__init__(self, value, verbose)
|
||||||
self._initial_value = value
|
self._initial_value = value
|
||||||
|
|
||||||
def release(self):
|
def release(self):
|
||||||
if self._value >= self._initial_value:
|
if self._value >= self._initial_value:
|
||||||
raise ValueError("Semaphore released too many times")
|
raise ValueError("Semaphore released too many times")
|
||||||
return _Semaphore.release(self)
|
return Semaphore.release(self)
|
||||||
|
|
||||||
|
|
||||||
def Event(*args, **kwargs):
|
class Event(_Verbose):
|
||||||
return _Event(*args, **kwargs)
|
|
||||||
|
|
||||||
class _Event(_Verbose):
|
|
||||||
|
|
||||||
# After Tim Peters' event class (without is_posted())
|
# After Tim Peters' event class (without is_posted())
|
||||||
|
|
||||||
|
@ -918,10 +906,7 @@ class Thread(_Verbose):
|
||||||
|
|
||||||
# The timer class was contributed by Itamar Shtull-Trauring
|
# The timer class was contributed by Itamar Shtull-Trauring
|
||||||
|
|
||||||
def Timer(*args, **kwargs):
|
class Timer(Thread):
|
||||||
return _Timer(*args, **kwargs)
|
|
||||||
|
|
||||||
class _Timer(Thread):
|
|
||||||
"""Call a function after a specified number of seconds:
|
"""Call a function after a specified number of seconds:
|
||||||
|
|
||||||
t = Timer(30.0, f, args=[], kwargs={})
|
t = Timer(30.0, f, args=[], kwargs={})
|
||||||
|
|
|
@ -237,6 +237,13 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #10968: Remove indirection in threading. The public names (Thread,
|
||||||
|
Condition, etc.) used to be factory functions returning instances of hidden
|
||||||
|
classes (_Thread, _Condition, etc.), because (if Guido recalls correctly) this
|
||||||
|
code pre-dates the ability to subclass extension types. It is now possible to
|
||||||
|
inherit from Thread and other classes, without having to import the private
|
||||||
|
underscored names like multiprocessing did.
|
||||||
|
|
||||||
- Issue #9723: Add shlex.quote functions, to escape filenames and command
|
- Issue #9723: Add shlex.quote functions, to escape filenames and command
|
||||||
lines.
|
lines.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue