'NameError: global name 'subprocess' is not defined' even though it is imported

'NameError: global name 'subprocess' is not defined' even though it is imported

当 运行 这个脚本时,我一直收到错误 NameError: global name 'subprocess' is not defined

我试过在 ProgramBody class 的 __init__ 函数中导入 subprocess,但这并没有改变错误。我也尝试过使用 from subprocess import check_output 但这只会给我相同的 NameError 但使用 check_output 而不是 subprocess.

出于绝望,我曾将 import 放在函数 get_speedset_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

您没有在线程开始或结束后加入线程的方法。

不过我遇到了一个例外

WindowsError: [Error 2] The system cannot find the file specified

这是因为我们需要告诉子进程在哪里可以找到可执行文件 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()

我已将流程修改为 ['ls', '-lah'] 并在 time.sleep 之前添加了以下内容并且它正常工作。

print self.current_speed
import sys
sys.stdout.flush()