【发布时间】:2014-12-09 02:31:54
【问题描述】:
我有一个使用线程计时器的脚本,它随机操作 2 个常见列表。
因为类实例操作线程计时器上的列表,我无法将变量传递给类并返回。
…类的所有实例都需要操作一个单一的、最新的列表。
因此,这些列表的范围设置为全局。但是,我需要范围在类级别,但要被多个类操作。
澄清一下……
这是基本的程序结构:
Global list variable_1
Global list variable_2
class MasterClass:
# this creates instances of the threaded classes.
There are 50+ instances of MasterClass creating thousands
of instances of ThreadedClass1, 2, & 3. All manipulate
global list variables 1 & 2.
class ThreadedClass1:
# threaded classes manipulate global list variables 1 & 2 on random timers.
class ThreadedClass2:
class ThreadedClass3:
问题:对于 MasterClass 的每个实例,我需要一个单独的列表变量 1 和 2。由该 MasterClass 实例调用的每个 ThreadedClasses 实例必须仅操作该 MasterClass 实例拥有的列表变量.
基本上我需要一个全局列表变量的等价物,但我需要它被 MasterClass 的一个实例封装,并由仅由该 MasterClass 实例调用的任何 ThreadedClasses 实例操作。
这是怎么做到的?
【问题讨论】:
标签: python multithreading global