[英]'NameError: global name 'subprocess' is not defined' even though it is imported
I keep on getting the error NameError: global name 'subprocess' is not defined
when running this script. 我继续收到错误NameError: global name 'subprocess' is not defined
运行此脚本时NameError: global name 'subprocess' is not defined
。
I've tried importing subprocess
inside the __init__
function of the ProgramBody
class, but that did not do anything to change the error. 我试过在ProgramBody
类的__init__
函数内部导入subprocess
ProgramBody
,但这并没有做任何更改错误的操作。 I've also tried to use from subprocess import check_output
but that would just give me the same NameError
but with check_output
instead of subprocess
. 我也试着使用from subprocess import check_output
但这只会给我同样的NameError
但check_output
而不是subprocess
。
I put the import
inside of the function get_speed
and set_speed
functions at one point out of desperation, and it worked; 我get_speed
地将import
到函数get_speed
和set_speed
函数的内部,它get_speed
处于绝望状态; but a solution such as that is very unfeasible as those functions will be called ~4 times a second. 但是像这样的解决方案是不可行的,因为这些功能每秒会被调用约4次。
Code 码
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()
Stacktrace 堆栈跟踪
(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 2.7.10
You dont have a method to join the thread after it starts or finishes. 您没有方法在线程启动或完成之后加入线程。
I do however get an exception 但是我确实有例外
WindowsError: [Error 2] The system cannot find the file specified WindowsError:[错误2]系统找不到指定的文件
This is because we need to tell subprocess where to find the executable nvidia-settings. 这是因为我们需要告诉子进程在哪里可以找到可执行的nvidia-settings。
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():
def __init__(self, interval=1):
self.thread = threading.Thread(target=self.run, args=())
self.thread.daemon = True
self.thread.start()
def run(self):
body.const_update()
def stop(self):
self.thread.join()
body = ProgramBody()
thread = ThreadingExample()
thread.stop()
I have modified the processes to be ['ls', '-lah'] and added before time.sleep the following and it works appropriately. 我已经将进程修改为['ls','-lah']并在time.sleep之前添加了以下内容,它可以正常工作。
print self.current_speed
import sys
sys.stdout.flush()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.