'NameError:全局名称'subprocess'未定义'即使已导入

时间:2016-03-03 05:54:49

标签: python python-2.7 nameerror

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

我已尝试在subprocess类的__init__函数中导入ProgramBody,但这并没有做任何更改错误的操作。我也尝试使用from subprocess import check_output,但这只会给我相同的NameErrorcheck_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

1 个答案:

答案 0 :(得分:2)

你没有一种方法可以在线程开始或结束后加入它。

但是我得到了例外

  

WindowsError:[错误2]系统找不到指定的文件

这是因为我们需要告诉子进程在哪里找到可执行文件的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' -& 39; -lah']并在time.sleep之前添加,并且它运作正常。

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