2016-03-03 350 views
1

運行此腳本時,我一直收到錯誤NameError: global name 'subprocess' is not defined'NameError:全局名稱'subprocess'未定義'即使已導入

我試過在ProgramBody類的__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

+0

哪個版本的python? – karthikr

+0

對不起,我忽略了,我正在使用Python 2.7.10 –

+0

那麼,子進程是在2.4中引入的,所以不知道該版本。另外,你可以用確切的堆棧跟蹤來編輯問題嗎? – karthikr

回答

2

你不必加入它的線程開始或結束後的方法。

然而,我得到一個異常

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() 
+0

此外,你不應該將body定義爲全局變量。在線程內實例化ProgramBody類。 –

+0

謝謝你的回答,但是我發現'thread.stop()'後面的任何命令都沒有執行,並且在它正常工作之前放置它們,並且完全刪除它似乎沒有任何副作用。你能解釋一下嗎? –

+0

thread.stop在那裏,以便腳本有一個端點。你的線程類需要一個方法來檢查線程是否結束,如果有,它會調用線程停止。 '如果線程。has_ended():thread.stop()' 如果你ctrl-c/z腳本。它需要一種加入垃圾收集所有線程的方式。否則,你可能會以殭屍進程結束。 此外,線程上的連接方法會殺死該線程。這就是爲什麼ProgramBody應該在你的線程中實例化的原因。這樣它就會自行清理。 –

相關問題