python - 访问类内常量全局范围变量的 Pythonic 方式?
问题描述
假设我有以下代码:
import threading
class Check(threading.Thread):
def __init__(self, item):
super(Check, self).__init__()
self._item = item
def run(self):
data = '{} ({} total)'.format(self._item, total)
collected.append(data)
items = ['item{}'.format(i) for i in xrange(5)]
total = len(items)
collected = []
for item in items: Check(item).start()
(让我们抛开上面代码的无用。它的唯一目的是帮助解释问题。)
有没有更 Pythonic 的方式来访问total
和collected
从类的run
方法内部访问Check
?的值total
保证不会改变。run
据我所知,我无法将任何其他内容传递给该方法。将相同的total
和collected
值传递给Check
类的构造函数似乎很愚蠢,但也许这是最佳实践?
解决方案
global
线程不是一个很好的组合。至少,如果你正在改变共享状态,你应该有锁,global
这是你可以拥有的“共享”状态的最极端情况。
我绝对建议将两者都传递item
给total
你的Check
类,然后从你的线程中检索值并稍后“组装”它:
import threading
class Check(threading.Thread):
def __init__(self, item, total):
super(Check, self).__init__()
self._item = item
self._total = total
self.result = None
def run(self):
data = '{} ({} total)'.format(self._item, self._total)
self.result = data
items = ['item{}'.format(i) for i in xrange(5)]
total = len(items)
collected = []
checks = [Check(item) for item in items]
for check in checks:
check.start()
for check in checks:
check.join()
collected = [check.result for check in checks]
但是如果你可以使用 Python 3,一个更好的方法是避免子类Thread
化并首先直接处理它们:
from concurrent.futures import ThreadPoolExecutor
def do_check(item, total):
data = '{} ({} total)'.format(self._item, self._total)
return data
items = ['item{}'.format(i) for i in range(5)]
total = len(items)
tpe = ThreadPoolExecutor(max_workers=5)
collected = list(tpe.map(lambda item: do_check(item, total), items))
没有global
s,没有线程,代码行数更少。
推荐阅读
- python - 在 python 烧瓶应用程序中找不到 google-cloud-sdk
- node.js - 如何使用 datetime INTERVAL 编写 nodeJS 查询以获取 postgreSQL 数据库的最后 24 小时记录
- java - JVM 堆堆栈在一段时间后变满
- node.js - 将订单ID附加到订单项目执行api后如何更新订单总额?(odoo12.0)
- r - 矩阵相关图
- javascript - Javascript Mongoose - 保存后正确关闭
- react-native - 如何避免因更改主题而更新清除/轮廓按钮样式
- azure - 使用 OAuth 2.0 从 Azure B2C AD 访问用户数据
- html - 当文本区域值未与检查元素一起显示时插入 html 代码
- php - PHP - 对数组值执行函数