有趣的问题——考虑到print 语句中发生的所有事情,包括softspace 属性的设置和检查,使其成为“线程安全”(实际上意味着:正在打印的线程只产生“控制标准输出”到另一个线程时打印换行符,因此保证输出的每一行都来自单个线程)有点挑战(实际线程的通常简单方法安全性——委托一个单独的线程专门“拥有”并处理sys.stdout,通过Queue.Queue与其通信——并不是那么有用,因为问题是不是线程安全[[即使是普通的print,也没有崩溃的风险,并且最终出现在标准输出上的字符正是那些被打印出来的字符]],但是线程之间需要互斥以扩展操作范围)。
所以,我想我做到了……:
import random
import sys
import thread
import threading
import time
def wait():
time.sleep(random.random())
return 'W'
def targ():
for n in range(8):
wait()
print 'Thr', wait(), thread.get_ident(), wait(), 'at', wait(), n
tls = threading.local()
class ThreadSafeFile(object):
def __init__(self, f):
self.f = f
self.lock = threading.RLock()
self.nesting = 0
def _getlock(self):
self.lock.acquire()
self.nesting += 1
def _droplock(self):
nesting = self.nesting
self.nesting = 0
for i in range(nesting):
self.lock.release()
def __getattr__(self, name):
if name == 'softspace':
return tls.softspace
else:
raise AttributeError(name)
def __setattr__(self, name, value):
if name == 'softspace':
tls.softspace = value
else:
return object.__setattr__(self, name, value)
def write(self, data):
self._getlock()
self.f.write(data)
if data == '\n':
self._droplock()
# comment the following statement out to get guaranteed chaos;-)
sys.stdout = ThreadSafeFile(sys.stdout)
thrs = []
for i in range(8):
thrs.append(threading.Thread(target=targ))
print 'Starting'
for t in thrs:
t.start()
for t in thrs:
t.join()
print 'Done'
对wait 的调用旨在保证在没有这种互斥保证的情况下(注释来源)的混乱混合输出。 使用包装,即上面的代码与它看起来完全一样,以及(至少)Python 2.5 及更高版本(我相信这也可以在早期版本中运行,但我没有任何很容易检查)输出是:
Thr W -1340583936 W at W 0
Thr W -1340051456 W at W 0
Thr W -1338986496 W at W 0
Thr W -1341116416 W at W 0
Thr W -1337921536 W at W 0
Thr W -1341648896 W at W 0
Thr W -1338454016 W at W 0
Thr W -1339518976 W at W 0
Thr W -1340583936 W at W 1
Thr W -1340051456 W at W 1
Thr W -1338986496 W at W 1
...more of the same...
“序列化”效果(即线程看起来像上面那样“很好地循环”)是一个副作用,即成为当前正在打印的线程比其他线程慢很多(所有那些等待!-)。注释掉wait中的time.sleep,输出改为
Thr W -1341648896 W at W 0
Thr W -1341116416 W at W 0
Thr W -1341648896 W at W 1
Thr W -1340583936 W at W 0
Thr W -1340051456 W at W 0
Thr W -1341116416 W at W 1
Thr W -1341116416 W at W 2
Thr W -1338986496 W at W 0
...more of the same...
即一个更典型的“多线程输出”……除了保证输出中的每一行完全来自一个线程。
当然,一个线程,例如,print 'ciao', 将保持对标准输出的“所有权”,直到它最终执行没有尾随逗号的打印,并且其他想要打印的线程可能睡眠一段时间(否则如何保证输出中的每一行都来自单个线程?嗯,一种架构是将部分行累积到线程本地存储而不是实际将它们写入标准输出,并且只做在收到\n... 时写信,我担心与softspace 设置正确交错,但可能可行)。