[英]Can I use python static variables to communicate between two modules?
I have two files, one that performs a step in an automated test and a second that stores and emails the test results. 我有两个文件,一个在自动化测试中执行一个步骤,另一个用于存储和通过电子邮件发送测试结果。 Both files are kicked off using a Hudson server and I need the first module to share information with the second, but when I access the static variables in the Reporter module, they have the initial values of 'None'.
两个文件都是使用Hudson服务器启动的,我需要第一个模块与第二个模块共享信息,但是当我访问Reporter模块中的静态变量时,它们的初始值为“None”。 I've cut the files down to the bare minimum in order to focus on getting the data sharing to work.
我已将文件减少到最低限度,以便专注于使数据共享工作。 File one is:
文件一是:
#!/usr/bin/python
from reports import Reporter
import time
class Flash_Device():
def Begin_Flashing( self ):
Reporter.flash_start_time = time.time()
if ( __name__ == "__main__" ):
flasher = Flash_Device()
flasher.Begin_Flashing()
The second file is the reporting file defined something like the code below: 第二个文件是报告文件,其定义类似于以下代码:
#!/usr/bin/python
class Reporter():
flash_start_time = None
def Report_Results( self ):
print "Flash start time:", Reporter.flash_start_time
if ( __name__ == "__main__" ):
reporter = Reporter()
reporter.Report_Results()
What I was hoping for is that I execute flasher.py, which updates the static variable flash_start_time, and then can access the updated value when executing reporter.py. 我希望的是我执行flashher.py,它更新静态变量flash_start_time,然后在执行reporter.py时可以访问更新的值。 Will that work if I make the method Begin_Flashing() static?
如果我将方法Begin_Flashing()设为静态,那会有用吗?
No. The problem is that only one module can be "__main__"
, therefore only one main stanza will be run. 不会。问题是只有一个模块可以是
"__main__"
,因此只会运行一个主要节。
There's nothing wrong with your code though, except that you import Monkey_Reporter
when you probably mean to import Reporter
. 有没有错,你的代码虽然,除了导入
Monkey_Reporter
当你大概的意思导入Reporter
。 And no, accessing it via reports
wouldn't change anything; 不,通过
reports
访问它不会改变任何东西; they're still the same class. 他们仍然是同一个班级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.