我正在使用多线程+类:
import time
class CounterThread(threading.Thread):
def __init__(self,queue, obj)
self.obj = obj
def run(self):
while True:
result = self.process()
self.out_queue.put(result)
time.sleep(self.obj['wait_Time'])
def process(self):
results = ''
subprocess.check_output(<command> , shell=True) #command for starting sample
time.sleep(self.obj['sample_Time'])
status_results = subprocess.check_output(<command>, shell=True) #reads sample output
lines = status_results.splitlines()
for lines in lines :
words = line.split()
if words[0] == 'Node' :
results += ('Node=' + words[1] + '\n')
else if words[0] == 'End-Time' :
results += 0'End-Time=' + words[1] + ' ' + words[2] + '\n')
subprocess.check_output(<command>, shell=True) #deletes sample
return results
行time.sleep(self.obj['sample_Time'])
是我收到unboundLocalError的地方。我很确定这是一个范围问题,但不知道如何解决它。如果我删除该行,代码运行正常。
以下是追溯:
Traceback (most recent call last):
File "/usr/software/lib/python2.7/threading.py", line 808 in __bootstrap_inner
self.run()
File "counter_extraction.py", line 29, in run
result = self.process()
File "counter_extract.py", line 42, in process
time.sleep(self.obj['sample_Time'])
UnboundLocalError: local variable 'time' referenced before assignment