栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Python 2.6中获得线程安全打印?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Python 2.6中获得线程安全打印?

有趣的问题-
考虑

print
语句中发生的所有事情,包括
softspace
属性的设置和检查,使其变为“线程安全”(实际上,这是指:正在打印的线程仅对另一个线程产生“对标准输出的控制”)打印一个换行符,以确保输出的每一行都来自一个线程)是一个挑战(通常简单易行的方法来实现
实际 线程安全-将一个单独的线程委派给“独占”并处理
sys.stdout
,通信)通过Queue.Queue到它-并不是那么有用,因为问题
不是 线程安全性[[即使是简单的
print

没有崩溃的风险,并且最终输出在标准输出上的字符恰好是打印出的字符]],但是对于扩展的操作范围,线程之间需要相互排斥)。

所以,我想我做到了:

import randomimport sysimport threadimport threadingimport timedef wait():  time.sleep(random.random())  return 'W'def targ():  for n in range(8):    wait()    print 'Thr', wait(), thread.get_ident(), wait(), 'at', wait(), ntls = 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'

在没有此互斥保证的情况下,调用to

wait
的目的是为了 保证 输出的混乱混合(因此请进行评论)。 通过
包装,即上面的代码与那里的样子完全一样,以及(至少)Python 2.5及更高版本(我相信这也可以在较早的版本中运行,但是我手头并不容易检查)输出为:

Thr W -1340583936 W at W 0Thr W -1340051456 W at W 0Thr W -1338986496 W at W 0Thr W -1341116416 W at W 0Thr W -1337921536 W at W 0Thr W -1341648896 W at W 0Thr W -1338454016 W at W 0Thr W -1339518976 W at W 0Thr W -1340583936 W at W 1Thr W -1340051456 W at W 1Thr W -1338986496 W at W 1  ...more of the same...

“串行化”效应(如上所示,线程看起来像“轮流”)是以下事实的副作用,即成为当前正在打印的线程比其他线程慢得多(所有这些等待!
-)。注释掉

time.sleep
in
wait
,输出改为

Thr W -1341648896 W at W 0Thr W -1341116416 W at W 0Thr W -1341648896 W at W 1Thr W -1340583936 W at W 0Thr W -1340051456 W at W 0Thr W -1341116416 W at W 1Thr W -1341116416 W at W 2Thr W -1338986496 W at W 0  ...more of the same...

即,一种更典型的“多线程输出” …,除了保证输出中的每一行完全来自一个线程。

当然,例如,执行此操作的线程

print 'ciao',

保留标准输出的“所有权”,直到它最终执行打印而没有结尾逗号为止,而其他想要打印的线程可能会休眠一段时间(否则,如何保证每个线程都可以休眠)输出中的行来自单个线程吗?好,一种架构是将部分行累积到线程本地存储,而不是实际将它们写入标准输出,并且仅在接收到
n
…时才进行写入,以便与
softspace
设置,我担心,但可能可行)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646974.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号