当前位置: 首页>>代码示例>>Python>>正文


Python thread._local方法代码示例

本文整理汇总了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) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:23,代码来源:test_thread_local.py

示例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 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:30,代码来源:test_thread.py

示例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) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:10,代码来源:test_thread_local.py

示例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) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:53,代码来源:test_thread_local.py


注:本文中的thread._local方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。