Threading Module源码概述(三)

时间:2023-03-08 16:56:58

Threading中的Thread

  threading中一个关键的组件是threading.Thread。

 

 class Thread(_Verbose):

     __initialized = False

     __exc_info = _sys.exc_info

     __exc_clear = _sys.exc_clear

     def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None): assert group is None, "group argument must be None for now"
_Verbose.__init__(self, verbose)
if kwargs is None:
kwargs = {}
self.__target = target
self.__name = str(name or _newname())
self.__args = args
self.__kwargs = kwargs
self.__daemonic = self._set_daemon()
self.__ident = None
self.__started = Event()
self.__stopped = False
self.__block = Condition(Lock())
self.__initialized = True
# sys.stderr is not stored in the class like
# sys.exc_info since it can be changed between instances
self.__stderr = _sys.stderr def _reset_internal_locks(self):
# private! Called by _after_fork() to reset our internal locks as
# they may be in an invalid state leading to a deadlock or crash.
if hasattr(self, '_Thread__block'): # DummyThread deletes self.__block
self.__block.__init__()
self.__started._reset_internal_locks() @property
def _block(self):
# used by a unittest
return self.__block def _set_daemon(self):
# Overridden in _MainThread and _DummyThread
return current_thread().daemon def __repr__(self):
assert self.__initialized, "Thread.__init__() was not called"
status = "initial"
if self.__started.is_set():
status = "started"
if self.__stopped:
status = "stopped"
if self.__daemonic:
status += " daemon"
if self.__ident is not None:
status += " %s" % self.__ident
return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status) def start(self): if not self.__initialized:
raise RuntimeError("thread.__init__() not called")
if self.__started.is_set():
raise RuntimeError("threads can only be started once")
if __debug__:
self._note("%s.start(): starting thread", self)
with _active_limbo_lock:
_limbo[self] = self
try:
_start_new_thread(self.__bootstrap, ())
except Exception:
with _active_limbo_lock:
del _limbo[self]
raise
self.__started.wait() def run(self): try:
if self.__target:
self.__target(*self.__args, **self.__kwargs)
finally:
# Avoid a refcycle if the thread is running a function with
# an argument that has a member that points to the thread.
del self.__target, self.__args, self.__kwargs def __bootstrap(self): try:
self.__bootstrap_inner()
except:
if self.__daemonic and _sys is None:
return
raise def _set_ident(self):
self.__ident = _get_ident() def __bootstrap_inner(self):
try:
self._set_ident()
self.__started.set()
with _active_limbo_lock:
_active[self.__ident] = self
del _limbo[self]
if __debug__:
self._note("%s.__bootstrap(): thread started", self) if _trace_hook:
self._note("%s.__bootstrap(): registering trace hook", self)
_sys.settrace(_trace_hook)
if _profile_hook:
self._note("%s.__bootstrap(): registering profile hook", self)
_sys.setprofile(_profile_hook) try:
self.run()
except SystemExit:
if __debug__:
self._note("%s.__bootstrap(): raised SystemExit", self)
except:
if __debug__:
self._note("%s.__bootstrap(): unhandled exception", self) if _sys and _sys.stderr is not None:
print>>_sys.stderr, ("Exception in thread %s:\n%s" %
(self.name, _format_exc()))
elif self.__stderr is not None: exc_type, exc_value, exc_tb = self.__exc_info()
try:
print>>self.__stderr, (
"Exception in thread " + self.name +
" (most likely raised during interpreter shutdown):")
print>>self.__stderr, (
"Traceback (most recent call last):")
while exc_tb:
print>>self.__stderr, (
' File "%s", line %s, in %s' %
(exc_tb.tb_frame.f_code.co_filename,
exc_tb.tb_lineno,
exc_tb.tb_frame.f_code.co_name))
exc_tb = exc_tb.tb_next
print>>self.__stderr, ("%s: %s" % (exc_type, exc_value)) finally:
del exc_type, exc_value, exc_tb
else:
if __debug__:
self._note("%s.__bootstrap(): normal return", self)
finally: self.__exc_clear()
finally:
with _active_limbo_lock:
self.__stop()
try:
# We don't call self.__delete() because it also
# grabs _active_limbo_lock.
del _active[_get_ident()]
except:
pass def __stop(self): if not hasattr(self, '_Thread__block'):
return
self.__block.acquire()
self.__stopped = True
self.__block.notify_all()
self.__block.release() def __delete(self):
"Remove current thread from the dict of currently running threads." try:
with _active_limbo_lock:
del _active[_get_ident()] except KeyError:
if 'dummy_threading' not in _sys.modules:
raise def join(self, timeout=None): if not self.__initialized:
raise RuntimeError("Thread.__init__() not called")
if not self.__started.is_set():
raise RuntimeError("cannot join thread before it is started")
if self is current_thread():
raise RuntimeError("cannot join current thread") if __debug__:
if not self.__stopped:
self._note("%s.join(): waiting until thread stops", self)
self.__block.acquire()
try:
if timeout is None:
while not self.__stopped:
self.__block.wait()
if __debug__:
self._note("%s.join(): thread stopped", self)
else:
deadline = _time() + timeout
while not self.__stopped:
delay = deadline - _time()
if delay <= 0:
if __debug__:
self._note("%s.join(): timed out", self)
break
self.__block.wait(delay)
else:
if __debug__:
self._note("%s.join(): thread stopped", self)
finally:
self.__block.release() @property
def name(self):
assert self.__initialized, "Thread.__init__() not called"
return self.__name @name.setter
def name(self, name):
assert self.__initialized, "Thread.__init__() not called"
self.__name = str(name) @property
def ident(self): assert self.__initialized, "Thread.__init__() not called"
return self.__ident def isAlive(self): assert self.__initialized, "Thread.__init__() not called"
return self.__started.is_set() and not self.__stopped is_alive = isAlive @property
def daemon(self): assert self.__initialized, "Thread.__init__() not called"
return self.__daemonic @daemon.setter
def daemon(self, daemonic):
if not self.__initialized:
raise RuntimeError("Thread.__init__() not called")
if self.__started.is_set():
raise RuntimeError("cannot set daemon status of active thread");
self.__daemonic = daemonic def isDaemon(self):
return self.daemon def setDaemon(self, daemonic):
self.daemon = daemonic def getName(self):
return self.name def setName(self, name):
self.name = name # The timer class was contributed by Itamar Shtull-Trauring

threading.Thread

  我们看到,在调用threading.Thread.start时,会在_limbo中记录线程,然后通过thread.start_new_thread创建原生线程,线程过程为_bootstrap,在_bootstrap中,会从_limbo中删除线程记录,转而将线程记录到_active中。然后调用run,通常用户从threading.Thread派生的class都会覆盖原有的run函数,这就实现了用户自定义的线程过程。

  在threading.Thread中,维护着一个Condition对象_block,在run结束后,start方法在最后会调用_stop操作。在_stop这个操作里会调用Condition对象的notifyAll函数self.__block.notify_all()。通过维护的一个__waiters列表来进行release操作,通知所有等待该对象的线程。那么会有那些线程会等待这个对象呢?凡是希望等待该线程结束消息的线程,都会通过threading.Thread.join方法里调用wait操作将线程注册到__waiters列表里成为Condition对象的等待线程。

def wait(self, timeout=None):

        if not self._is_owned():
raise RuntimeError("cannot wait on un-acquired lock")
waiter = _allocate_lock()
waiter.acquire()
self.__waiters.append(waiter) #将等待线程添加到维护的列表中被join调用
saved_state = self._release_save()

__block.wait()