運行此腳本時,我一直收到錯誤NameError: global name 'subprocess' is not defined
。'NameError:全局名稱'subprocess'未定義'即使已導入
我試過在ProgramBody
類的__init__
函數裏面導入subprocess
,但是沒有做任何改變錯誤的操作。我也嘗試使用from subprocess import check_output
,但這隻會給我相同的NameError
,但check_output
而不是subprocess
。
我把import
功能get_speed
和set_speed
功能在絕望的一個點上,它的工作;但是像這樣的解決方案是非常不可行的,因爲這些功能將被稱爲每秒4次。
代碼
import subprocess
import time
import threading
class ProgramBody():
def __init__(self):
self.current_speed = 0
self.current_temp = 0
self.update = True
self.change = True
self.update_wait = 0.25
def get_speed (self):
return subprocess.check_output (["nvidia-settings", "-tq", "[fan:0]/GPUCurrentFanSpeed"])
def set_speed (self, speed):
subprocess.call (["nvidia-settings", "-a", "[gpu:0]/GPUFanControlState=1", "-a", "[fan:0]/GPUTargetFanSpeed=" + str(speed)])
def get_temp (self):
return subprocess.check_output (["nvidia-settings", "-tq", "[gpu:0]/GPUCoreTemp"])
def const_update(self):
while self.update:
self.current_speed = self.get_speed()
self.current_temp = self.get_temp()
time.sleep (self.update_wait)
class ThreadingExample(object):
def __init__(self, interval=1):
thread = threading.Thread(target=self.run, args=())
thread.daemon = True
thread.start()
def run(self):
body.const_update()
body = ProgramBody()
thread = ThreadingExample()
堆棧跟蹤
(Pdb) Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "FanSpeed.py", line 42, in run
body.const_update()
File "FanSpeed.py", line 32, in const_update
self.current_temp = self.get_temp()
File "FanSpeed.py", line 27, in get_temp
return subprocess.check_output (["nvidia-settings", "-tq", "[gpu:0]/GPUCoreTemp"])
NameError: global name 'subprocess' is not defined
的Python 2.7.10
哪個版本的python? – karthikr
對不起,我忽略了,我正在使用Python 2.7.10 –
那麼,子進程是在2.4中引入的,所以不知道該版本。另外,你可以用確切的堆棧跟蹤來編輯問題嗎? – karthikr