本文整理汇总了Python中thread._local方法的典型用法代码示例。如果您正苦于以下问题:Python thread._local方法的具体用法?Python thread._local怎么用?Python thread._local使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类thread
的用法示例。
在下文中一共展示了thread._local方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_local
# 需要导入模块: import thread [as 别名]
# 或者: from thread import _local [as 别名]
def test_local(self):
mydata = local()
mydata.number = 42
self.assertEqual(mydata.number,42)
self.assertEqual(mydata.__dict__,{'number': 42})
mydata.__dict__.setdefault('widgets', [])
self.assertEqual(mydata.widgets,[])
log=[]
def f():
items = mydata.__dict__.items()
items.sort()
log.append(items)
mydata.number = 11
log.append(mydata.number)
thread = threading.Thread(target=f)
thread.start()
thread.join()
self.assertEqual(log,[[], 11])
self.assertEqual(mydata.number,42)
示例2: test_thread_local
# 需要导入模块: import thread [as 别名]
# 或者: from thread import _local [as 别名]
def test_thread_local(self):
import thread
from System.Threading import Thread
x = thread._local()
#--Sanity
x.foo = 42
self.assertEqual(x.foo, 42)
global found
found = None
def f():
global found
found = hasattr(x, 'foo')
thread.start_new_thread(f, ())
while found == None:
Thread.Sleep(100)
self.assertTrue(not found)
self.assertEqual(x.__dict__, {'foo': 42})
try:
x.__dict__ = None
self.fail("Should not be able to set thread._local().__dict__!")
except AttributeError, e:
pass
示例3: test_two_locals
# 需要导入模块: import thread [as 别名]
# 或者: from thread import _local [as 别名]
def test_two_locals(self):
'''Ensures that two locals in the same thread have separate dicts.'''
first = local()
first.x = 7
second = local()
second.x = 12
self.assertEquals(7, first.x)
self.assertEquals(12, second.x)
示例4: test_subclass_local
# 需要导入模块: import thread [as 别名]
# 或者: from thread import _local [as 别名]
def test_subclass_local(self):
def f():
items = mydata.__dict__.items()
items.sort()
log.append(items)
mydata.number = 11
log.append(mydata.number)
class MyLocal(local):
number = 2
initialized = False
def __init__(self, **kw):
if self.initialized:
raise SystemError('__init__ called too many times')
self.initialized = True
self.__dict__.update(kw)
def squared(self):
return self.number ** 2
class SubSubLocal(MyLocal):
pass
mydata = MyLocal(color='red')
self.assertEqual(mydata.number,2)
self.assertEqual(mydata.color,'red')
del mydata.color
log=[]
self.assertEqual(mydata.squared(),4)
thread = threading.Thread(target=f)
thread.start()
thread.join()
self.assertEqual(log,[[('color', 'red'), ('initialized', True)], 11])
self.assertEqual(mydata.number,2)
self.assertRaises(TypeError, local, 'any arguments')
SubSubLocal(color='red')
def accessColor():
mydata.color
self.assertRaises(AttributeError,accessColor)
class MyLocal(local):
__slots__ = 'number'
mydata = MyLocal()
mydata.number = 42
mydata.color = 'red'
thread = threading.Thread(target=f)
thread.start()
thread.join()
self.assertEqual(mydata.number,11)