11.4. threading — 管理单个进程里的并行操作 | 并行运算 |《python 3 标准库实例教程》| python 技术论坛-380玩彩网官网入口
目的:管理几个线程执行。
使用线程允许一个程序在同一个进程空间中并发运行多个操作。
thread 对象
最简单的使用一个 thread
是去使用一个目标函数实例化它,然后调用 start()
让线程运行。
threading_simple.py
import threading
def worker():
"""thread worker function"""
print('worker')
threads = []
for i in range(5):
t = threading.thread(target=worker)
threads.append(t)
t.start()
输出是五行 worker
。
$ python3 threading_simple.py
worker
worker
worker
worker
worker
能够创建一个线程并且传递给它参数告诉它做什么是很有用的。任何对象都可以作为参数传递给线程。这个例子传递了一个数字,然后在线程中打印出来。
threading_simpleargs.py
import threading
def worker(num):
"""thread worker function"""
print('worker: %s' % num)
threads = []
for i in range(5):
t = threading.thread(target=worker, args=(i,))
threads.append(t)
t.start()
整数参数现在包括在每行的输出消息中。
$ python3 threading_simpleargs.py
worker: 0
worker: 1
worker: 2
worker: 3
worker: 4
确定当前线程
使用参数或者名字来标识出线程的方法并不优雅且也没有必要。每个线程 thread
实例自带一个名字,这个名字也可以在线程创建时更改。如果你要在多段运行中使用多个线程处理不同的操作,务必不要忘记给每个线程命名名字。
threading_names.py
import threading
import time
def worker():
print(threading.current_thread().getname(), 'starting')
time.sleep(0.2)
print(threading.current_thread().getname(), 'exiting')
def my_service():
print(threading.current_thread().getname(), 'starting')
time.sleep(0.3)
print(threading.current_thread().getname(), 'exiting')
t = threading.thread(name='my_service', target=my_service)
w = threading.thread(name='worker', target=worker)
w2 = threading.thread(target=worker) # 使用默认名字
w.start()
w2.start()
t.start()
调试的输出信息是各个线程当前的名字。 线程名字叫 "thread-1"
的是我们并未命名的 w2
线程的名字。
$ python3 threading_names.py
worker starting
thread-1 starting
my_service starting
worker exiting
thread-1 exiting
my_service exiting
大多数程序都不会用 print
来进行调试。 模块支持在日志信息中写入线程的名字,你可以用格式化代码 %(threadname)s
来得到它。日志信息中写入了线程名字可以更好得定位源代码中相关代码。
threading_names_log.py
import logging
import threading
import time
def worker():
logging.debug('starting')
time.sleep(0.2)
logging.debug('exiting')
def my_service():
logging.debug('starting')
time.sleep(0.3)
logging.debug('exiting')
logging.basicconfig(
level=logging.debug,
format='[%(levelname)s] (%(threadname)-10s) %(message)s',
)
t = threading.thread(name='my_service', target=my_service)
w = threading.thread(name='worker', target=worker)
w2 = threading.thread(target=worker) # 使用默认名字
w.start()
w2.start()
t.start()
同时也是线程安全的,所以即使是不同线程中的信息也会保证清晰明确。
$ python3 threading_names_log.py
[debug] (worker ) starting
[debug] (thread-1 ) starting
[debug] (my_service) starting
[debug] (worker ) exiting
[debug] (thread-1 ) exiting
[debug] (my_service) exiting
守护 vs. 非守护
说到这点,例子中隐式得等待了所有线程执行完成后退出。有时程序所生成的线程是 守护 状态的,也就是说不会阻塞主线程退出。守护线程可以用在那些不容易从线程中退出的服务中。或者那些要让线程在运行中退出但不会丢失或者污染其他数据的服务(比如某个线程负责发起「心跳检测」)。把某线程标记为守护线程,可以在构建时传入 daemon=true
或调用 set_daemon()
设置为 true
。默认情况下,线程是非守护的。
threading_daemon.py
import threading
import time
import logging
def daemon():
logging.debug('starting')
time.sleep(0.2)
logging.debug('exiting')
def non_daemon():
logging.debug('starting')
logging.debug('exiting')
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
d = threading.thread(name='daemon', target=daemon, daemon=true)
t = threading.thread(name='non-daemon', target=non_daemon)
d.start()
t.start()
设置为守护的线程我们可以看到并未打印出"exiting"
,因为所有非守护线程(包括主线程)在守护线程被 sleep()
唤醒前就结束了。
$ python3 threading_daemon.py
(daemon ) starting
(non-daemon) starting
(non-daemon) exiting
如果要等待标记为守护的线程结束,可以使用 join()
方法。
threading_daemon_join.py
import threading
import time
import logging
def daemon():
logging.debug('starting')
time.sleep(0.2)
logging.debug('exiting')
def non_daemon():
logging.debug('starting')
logging.debug('exiting')
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
d = threading.thread(name='daemon', target=daemon, daemon=true)
t = threading.thread(name='non-daemon', target=non_daemon)
d.start()
t.start()
d.join()
t.join()
使用 join()
后,守护线程就有机会执行完成并打印出 "exiting"
。
$ python3 threading_daemon_join.py
(daemon ) starting
(non-daemon) starting
(non-daemon) exiting
(daemon ) exiting
默认情况下,join()
会无限期阻塞直到线程完成。我们也可以传递一个浮点数来表示我们阻塞的秒数。如果在超时时间内线程并未结束,join()
就会返回,不再继续等待。
threading_daemon_join_timeout.py
import threading
import time
import logging
def daemon():
logging.debug('starting')
time.sleep(0.2)
logging.debug('exiting')
def non_daemon():
logging.debug('starting')
logging.debug('exiting')
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
d = threading.thread(name='daemon', target=daemon, daemon=true)
t = threading.thread(name='non-daemon', target=non_daemon)
d.start()
t.start()
d.join(0.1)
print('d.is_alive()', d.is_alive())
t.join()
因为超时时间小于守护线程的睡眠时间,在 join()
返回后该线程仍然是 「存活」状态。
$ python3 threading_daemon_join_timeout.py
(daemon ) starting
(non-daemon) starting
(non-daemon) exiting
d.is_alive() true
枚举所有线程
没必要为所有守护线程保留一个显示句柄以确保他们在主进程退出之前已经完成。 enumerate()
返回一个激活的 thread
实例列表。这个列表包括当前线程,由于加入当前线程会导致死锁情况,所以应该跳过。
threading_enumerate.py
import random
import threading
import time
import logging
def worker():
"""thread worker function"""
pause = random.randint(1, 5) / 10
logging.debug('sleeping %0.2f', pause)
time.sleep(pause)
logging.debug('ending')
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
for i in range(3):
t = threading.thread(target=worker, daemon=true)
t.start()
main_thread = threading.main_thread()
for t in threading.enumerate():
if t is main_thread:
continue
logging.debug('joining %s', t.getname())
t.join()
由于 worker 随机休眠了一些时间,所以输出可能会有所变化。
$ python3 threading_enumerate.py
(thread-1 ) sleeping 0.20
(thread-2 ) sleeping 0.30
(thread-3 ) sleeping 0.40
(mainthread) joining thread-1
(thread-1 ) ending
(mainthread) joining thread-3
(thread-2 ) ending
(thread-3 ) ending
(mainthread) joining thread-2
继承 thread
在启动的时候,thread
做了一些基本的初始化然后调用了 run()
方法,他然后调用了传入构造器的目标函数。为了创建 thread
的子类,可以重写 run()
然后做任何你想做的。
threading_subclass.py
import threading
import logging
class mythread(threading.thread):
def run(self):
logging.debug('running')
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
for i in range(5):
t = mythread()
t.start()
run()
的返回值被忽略了。
$ python3 threading_subclass.py
(thread-1 ) running
(thread-2 ) running
(thread-3 ) running
(thread-4 ) running
(thread-5 ) running
由于 传入 thread
构造函数的 args
和 kwargs
被保存在以 --
开头的私有属性中,所以不能在子类中访问到。为了传入参数给自定义的线程类型,可以重新定义构造函数,把参数保存在能够在子类中看到的实例属性中。
threading_subclass_args.py
import threading
import logging
class mythreadwithargs(threading.thread):
def __init__(self, group=none, target=none, name=none,
args=(), kwargs=none, *, daemon=none):
super().__init__(group=group, target=target, name=name,
daemon=daemon)
self.args = args
self.kwargs = kwargs
def run(self):
logging.debug('running with %s and %s',
self.args, self.kwargs)
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
for i in range(5):
t = mythreadwithargs(args=(i,), kwargs={'a': 'a', 'b': 'b'})
t.start()
mythreadwithargs
使用了和 thread
相同的 api,但 mythreadwithargs
相对 thread
可以轻松地改变改造函数去更直接地获取到更多不同的参数,就像其他类一样。
$ python3 threading_subclass_args.py
(thread-1 ) running with (0,) and {'b': 'b', 'a': 'a'}
(thread-2 ) running with (1,) and {'b': 'b', 'a': 'a'}
(thread-3 ) running with (2,) and {'b': 'b', 'a': 'a'}
(thread-4 ) running with (3,) and {'b': 'b', 'a': 'a'}
(thread-5 ) running with (4,) and {'b': 'b', 'a': 'a'}
计时器线程
timer
提供了一个继承 thread
的例子,也包含在 threading
模块中。timer
在延迟一段时间后启动工作,他可以在延迟的这段时间内任何时间点取消。
threading_timer.py
import threading
import time
import logging
def delayed():
logging.debug('worker running')
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
t1 = threading.timer(0.3, delayed)
t1.setname('t1')
t2 = threading.timer(0.3, delayed)
t2.setname('t2')
logging.debug('starting timers')
t1.start()
t2.start()
logging.debug('waiting before canceling %s', t2.getname())
time.sleep(0.2)
logging.debug('canceling %s', t2.getname())
t2.cancel()
logging.debug('done')
例子中的第二个计时器从来不会运行,并且第一个计时器好像是在主程序完成之后运行的。由于它不是守护线程,但是它在主线程完成时被隐式加入了。
$ python3 threading_timer.py
(mainthread) starting timers
(mainthread) waiting before canceling t2
(mainthread) canceling t2
(mainthread) done
(t1 ) worker running
线程间的信号
虽然我们使用多线程是为了同时运行多个操作,不过有时我们也需要同步它们。在线程间安全通信的方式可以使用事件对象。每个 event
(事件)内部都有一个标记,我们可以用 set()
和 clear()
方法控制它。其他线程可以使用 wait()
来暂停直到标记被设置才重新启动,使用这方法可以有效阻塞执行。
threading_event.py
import logging
import threading
import time
def wait_for_event(e):
"""做任何事前先等待事件被设置。"""
logging.debug('wait_for_event starting')
event_is_set = e.wait()
logging.debug('event set: %s', event_is_set)
def wait_for_event_timeout(e, t):
"""等待 t 秒。"""
while not e.is_set():
logging.debug('wait_for_event_timeout starting')
event_is_set = e.wait(t)
logging.debug('event set: %s', event_is_set)
if event_is_set:
logging.debug('processing event')
else:
logging.debug('doing other work')
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
e = threading.event()
t1 = threading.thread(
name='block',
target=wait_for_event,
args=(e,),
)
t1.start()
t2 = threading.thread(
name='nonblock',
target=wait_for_event_timeout,
args=(e, 2),
)
t2.start()
logging.debug('waiting before calling event.set()')
time.sleep(0.3)
e.set()
logging.debug('event is set')
wait()
方法可以接收一个参数,表示事件等待的超时时间。同时它返回一个布尔类型的对象,指代事件被设置了没有,所以我们可以根据它的返回来进行下一步行动。 事件中的 is_set()
方法可以单独使用而不必担心阻塞住。
本例中, wait_for_event_timeout()
会检测事件的状态但并不会一直阻塞。 wait_for_event()
则因调用的是 wait()
而阻塞住直到事件状态改变才会返回。
$ python3 threading_event.py
(block ) wait_for_event starting
(nonblock ) wait_for_event_timeout starting
(mainthread) waiting before calling event.set()
(mainthread) event is set
(nonblock ) event set: true
(nonblock ) processing event
(block ) event set: true
控制资源访问
除了同步多个线程的操作,控制共享资源的访问以防止污染或丢失数据也是非常重要的。 python 的内置数据结构(列表(list),字典(dict)等....)都是线程安全的,有「原子操作」的对象都是这样。(全局解释器锁会保护这样的 python 内部数据结构在更新时线程不会被释放)。其他 python 的数据结构或者说较简单的类型如整数浮点数则不会受此保护。我们可以使用 lock
对象来保护某对象的访问。
threading_lock.py
import logging
import random
import threading
import time
class counter:
def __init__(self, start=0):
self.lock = threading.lock()
self.value = start
def increment(self):
logging.debug('waiting for lock')
self.lock.acquire()
try:
logging.debug('acquired lock')
self.value = self.value 1
finally:
self.lock.release()
def worker(c):
for i in range(2):
pause = random.random()
logging.debug('sleeping %0.02f', pause)
time.sleep(pause)
c.increment()
logging.debug('done')
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
counter = counter()
for i in range(2):
t = threading.thread(target=worker, args=(counter,))
t.start()
logging.debug('waiting for worker threads')
main_thread = threading.main_thread()
for t in threading.enumerate():
if t is not main_thread:
t.join()
logging.debug('counter: %d', counter.value)
本例中,worker()
函数会调用 counter
实例中的 increment
,这里面管理着一把 lock
来防止两个线程在同一时间内改变内部状态。如果没有用 lock
,很可能会丢失 value 属性的值。
$ python3 threading_lock.py
(thread-1 ) sleeping 0.18
(thread-2 ) sleeping 0.93
(mainthread) waiting for worker threads
(thread-1 ) waiting for lock
(thread-1 ) acquired lock
(thread-1 ) sleeping 0.11
(thread-1 ) waiting for lock
(thread-1 ) acquired lock
(thread-1 ) done
(thread-2 ) waiting for lock
(thread-2 ) acquired lock
(thread-2 ) sleeping 0.81
(thread-2 ) waiting for lock
(thread-2 ) acquired lock
(thread-2 ) done
(mainthread) counter: 4
从当前线程中得知锁是否被其他线程占用可以向 acquire()
传递 false
来替换 阻塞
的原参数来立即得知。下个例子中,worker()
会尝试获得三次锁,并计算总共尝试了几次才获得这三次锁。同时,lock_holder()
的循环会不断获取并释放锁,每次都有一小段间隔来模拟「正在加载...」。
threading_lock_noblock.py
import logging
import threading
import time
def lock_holder(lock):
logging.debug('starting')
while true:
lock.acquire()
try:
logging.debug('holding')
time.sleep(0.5)
finally:
logging.debug('not holding')
lock.release()
time.sleep(0.5)
def worker(lock):
logging.debug('starting')
num_tries = 0
num_acquires = 0
while num_acquires < 3:
time.sleep(0.5)
logging.debug('trying to acquire')
have_it = lock.acquire(0)
try:
num_tries = 1
if have_it:
logging.debug('iteration %d: acquired',
num_tries)
num_acquires = 1
else:
logging.debug('iteration %d: not acquired',
num_tries)
finally:
if have_it:
lock.release()
logging.debug('done after %d iterations', num_tries)
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
lock = threading.lock()
holder = threading.thread(
target=lock_holder,
args=(lock,),
name='lockholder',
daemon=true,
)
holder.start()
worker = threading.thread(
target=worker,
args=(lock,),
name='worker',
)
worker.start()
我们可以看到,worker()
为了获取三次锁而尝试了5次。
$ python3 threading_lock_noblock.py
(lockholder) starting
(lockholder) holding
(worker ) starting
(lockholder) not holding
(worker ) trying to acquire
(worker ) iteration 1: acquired
(lockholder) holding
(worker ) trying to acquire
(worker ) iteration 2: not acquired
(lockholder) not holding
(worker ) trying to acquire
(worker ) iteration 3: acquired
(lockholder) holding
(worker ) trying to acquire
(worker ) iteration 4: not acquired
(lockholder) not holding
(worker ) trying to acquire
(worker ) iteration 5: acquired
(worker ) done after 5 iterations
可重入锁
普通的 lock
对象即使是在同一个线程中也不能被获取两次。如果在一次调用链中多个函数都需要一把锁,使用普通锁可以说是灾难性的。
threading_lock_reacquire.py
import threading
lock = threading.lock()
print('first try :', lock.acquire())
print('second try:', lock.acquire(0))
上面的例子中,我们要预先给 acquire()
传入 0 来防止它一直阻塞(因为已经被获取了嘛)。
$ python3 threading_lock_reacquire.py
first try : true
second try: false
如果在同一个线程中多个地方都需要一把锁,我们可以使用可重入的 rlock
来代替 lock
。
threading_rlock.py
import threading
lock = threading.rlock()
print('first try :', lock.acquire())
print('second try:', lock.acquire(0))
只要把 lock
变成 rlock
就可以啦。(译注:acquire()
两次,release()
也要两次喔。)
$ python3 threading_rlock.py
first try : true
second try: true
锁的上下文管理器
锁自带上下文管理器 api 同时兼容 with
语句。 使用 with
我们可以不必写出显式的获取和释放。
threading_lock_with.py
import threading
import logging
def worker_with(lock):
with lock:
logging.debug('lock acquired via with')
def worker_no_with(lock):
lock.acquire()
try:
logging.debug('lock acquired directly')
finally:
lock.release()
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
lock = threading.lock()
w = threading.thread(target=worker_with, args=(lock,))
nw = threading.thread(target=worker_no_with, args=(lock,))
w.start()
nw.start()
worker_with()
和 worker_no_with()
所实现的功能一模一样的。
$ python3 threading_lock_with.py
(thread-1 ) lock acquired via with
(thread-2 ) lock acquired directly
同步线程
除了使用 events
,另一种同步线程的方法是使用 condition
对象。 condition
使用了 lock
,所以它会绑定共享的资源,也就会让多个线程等待资源更新完成。下面的例子中 consumer()
在继续进行前会等待 condition
设置完成。producer()
线程则负责设置 condition
并通知其他线程可以继续了。
threading_condition.py
import logging
import threading
import time
def consumer(cond):
"""等待 condition,之后消耗资源。"""
logging.debug('starting consumer thread')
with cond:
cond.wait()
logging.debug('resource is available to consumer')
def producer(cond):
"""设置 consumer 所需的资源"""
logging.debug('starting producer thread')
with cond:
logging.debug('making resource available')
cond.notifyall()
logging.basicconfig(
level=logging.debug,
format='%(asctime)s (%(threadname)-2s) %(message)s',
)
condition = threading.condition()
c1 = threading.thread(name='c1', target=consumer,
args=(condition,))
c2 = threading.thread(name='c2', target=consumer,
args=(condition,))
p = threading.thread(name='p', target=producer,
args=(condition,))
c1.start()
time.sleep(0.2)
c2.start()
time.sleep(0.2)
p.start()
我们使用了上节了解的到 with
用法。 直接用 acquire()
和 release()
也是一样的效果。
$ python3 threading_condition.py
2016-07-10 10:45:28,170 (c1) starting consumer thread
2016-07-10 10:45:28,376 (c2) starting consumer thread
2016-07-10 10:45:28,581 (p ) starting producer thread
2016-07-10 10:45:28,581 (p ) making resource available
2016-07-10 10:45:28,582 (c1) resource is available to consumer
2016-07-10 10:45:28,582 (c2) resource is available to consumer
「屏障」(barrier)是另一种线程同步的机制。
每个 barrier
会建立起一个控制点,所有处在其中的线程都会被阻塞,直到所有的线程都到达这个控制点。它会让所有的线程单独启动,然后在它们全都准备好执行下一步前先阻塞住。
threading_barrier.py
import threading
import time
def worker(barrier):
print(threading.current_thread().name,
'waiting for barrier with {} others'.format(
barrier.n_waiting))
worker_id = barrier.wait()
print(threading.current_thread().name, 'after barrier',
worker_id)
num_threads = 3
barrier = threading.barrier(num_threads)
threads = [
threading.thread(
name='worker-%s' % i,
target=worker,
args=(barrier,),
)
for i in range(num_threads)
]
for t in threads:
print(t.name, 'starting')
t.start()
time.sleep(0.1)
for t in threads:
t.join()
本例中,barrier
被用来阻塞住三个线程。当条件适宜,所有的线程都会在同一时间在那个阻塞的点被释放。 wait()
的返回值表示所释放的是哪个线程,这样我们就可以用来限制一些线程的操作比如清理某共享资源。
$ python3 threading_barrier.py
worker-0 starting
worker-0 waiting for barrier with 0 others
worker-1 starting
worker-1 waiting for barrier with 1 others
worker-2 starting
worker-2 waiting for barrier with 2 others
worker-2 after barrier 2
worker-0 after barrier 0
worker-1 after barrier 1
barrier
的 abort()
方法会导致所有等待中的线程接收到一个 brokenbarriererror
。 我们可以使用此方法来告知那些被阻塞住的线程该结束了。
threading_barrier_abort.py
import threading
import time
def worker(barrier):
print(threading.current_thread().name,
'waiting for barrier with {} others'.format(
barrier.n_waiting))
try:
worker_id = barrier.wait()
except threading.brokenbarriererror:
print(threading.current_thread().name, 'aborting')
else:
print(threading.current_thread().name, 'after barrier',
worker_id)
num_threads = 3
barrier = threading.barrier(num_threads 1)
threads = [
threading.thread(
name='worker-%s' % i,
target=worker,
args=(barrier,),
)
for i in range(num_threads)
]
for t in threads:
print(t.name, 'starting')
t.start()
time.sleep(0.1)
barrier.abort()
for t in threads:
t.join()
这次我们将 barrier
设置成比实际开始的线程多一个,这样所有的线程就会被阻塞住,我们调用 abort()
就可以引起 brokenbarriererror
了。
$ python3 threading_barrier_abort.py
worker-0 starting
worker-0 waiting for barrier with 0 others
worker-1 starting
worker-1 waiting for barrier with 1 others
worker-2 starting
worker-2 waiting for barrier with 2 others
worker-0 aborting
worker-2 aborting
worker-1 aborting
限制并发访问
有时我们需要允许多个工作函数在同一时间访问同一个资源,但我们也要限制可访问的总数。比如,一个连接池支持的是固定大小的连接量,一个网络应用支持的也是固定的并发下载量。基于这样的情况,使用 semaphore
来管理这些连接是其中的一种方式。
threading_semaphore.py
import logging
import random
import threading
import time
class activepool:
def __init__(self):
super(activepool, self).__init__()
self.active = []
self.lock = threading.lock()
def makeactive(self, name):
with self.lock:
self.active.append(name)
logging.debug('running: %s', self.active)
def makeinactive(self, name):
with self.lock:
self.active.remove(name)
logging.debug('running: %s', self.active)
def worker(s, pool):
logging.debug('waiting to join the pool')
with s:
name = threading.current_thread().getname()
pool.makeactive(name)
time.sleep(0.1)
pool.makeinactive(name)
logging.basicconfig(
level=logging.debug,
format='%(asctime)s (%(threadname)-2s) %(message)s',
)
pool = activepool()
s = threading.semaphore(2)
for i in range(4):
t = threading.thread(
target=worker,
name=str(i),
args=(s, pool),
)
t.start()
例子中, activepool
类只是用来追踪给定时刻下哪些线程在工作的。如果是实际情况中,资源池一般还要分配连接或者其他值给新的活动线程,并且当线程结束后回收这些值。在这个例子中,它只被用来显示线程的名字以表示同时最多只有两个线程在工作。
$ python3 threading_semaphore.py
2016-07-10 10:45:29,398 (0 ) waiting to join the pool
2016-07-10 10:45:29,398 (0 ) running: ['0']
2016-07-10 10:45:29,399 (1 ) waiting to join the pool
2016-07-10 10:45:29,399 (1 ) running: ['0', '1']
2016-07-10 10:45:29,399 (2 ) waiting to join the pool
2016-07-10 10:45:29,399 (3 ) waiting to join the pool
2016-07-10 10:45:29,501 (1 ) running: ['0']
2016-07-10 10:45:29,501 (0 ) running: []
2016-07-10 10:45:29,502 (3 ) running: ['3']
2016-07-10 10:45:29,502 (2 ) running: ['3', '2']
2016-07-10 10:45:29,607 (3 ) running: ['2']
2016-07-10 10:45:29,608 (2 ) running: []
线程特定数据
当某些资源需要被锁定才能让多个线程使用,其他资源需要被保护以让并不拥有它的线程使用它时,local()
类就可以在每个线程中创建一个用于隐藏值的对象容器。
threading_local.py
import random
import threading
import logging
def show_value(data):
try:
val = data.value
except attributeerror:
logging.debug('no value yet')
else:
logging.debug('value=%s', val)
def worker(data):
show_value(data)
data.value = random.randint(1, 100)
show_value(data)
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
local_data = threading.local()
show_value(local_data)
local_data.value = 1000
show_value(local_data)
for i in range(2):
t = threading.thread(target=worker, args=(local_data,))
t.start()
local_data.value
在当前的线程设置任何值前,对于当前线程来说它都什么都没有。
$ python3 threading_local.py
(mainthread) no value yet
(mainthread) value=1000
(thread-1 ) no value yet
(thread-1 ) value=33
(thread-2 ) no value yet
(thread-2 ) value=74
如果想设置一个初始值,需要我们继承后自己重写 __init__()
方法。
threading_local_defaults.py
import random
import threading
import logging
def show_value(data):
try:
val = data.value
except attributeerror:
logging.debug('no value yet')
else:
logging.debug('value=%s', val)
def worker(data):
show_value(data)
data.value = random.randint(1, 100)
show_value(data)
class mylocal(threading.local):
def __init__(self, value):
super().__init__()
logging.debug('initializing %r', self)
self.value = value
logging.basicconfig(
level=logging.debug,
format='(%(threadname)-10s) %(message)s',
)
local_data = mylocal(1000)
show_value(local_data)
for i in range(2):
t = threading.thread(target=worker, args=(local_data,))
t.start()
只要设置了初始值,__init__()
的调用都是同一个对象(注意 id()
的值)。
$ python3 threading_local_defaults.py
(mainthread) initializing <__main__.mylocal object at
0x101c6c288>
(mainthread) value=1000
(thread-1 ) initializing <__main__.mylocal object at
0x101c6c288>
(thread-1 ) value=1000
(thread-1 ) value=18
(thread-2 ) initializing <__main__.mylocal object at
0x101c6c288>
(thread-2 ) value=1000
(thread-2 ) value=77
参考
thread
-- 底层线程 api。queue
-- 线程间传递信息首选 -- 线程安全的队列。- --
像threading
一样的进程 api 模块。
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 cc 协议,如果我们的工作有侵犯到您的权益,请及时联系380玩彩网官网入口。