2012-11-30 42 views
0

我試着在Python子進程來執行的程序:的Python:例外在線程

class MiThread(threading.Thread): 
     def __init__(self): 
      threading.Thread.__init__(self) 

     def run(self): 
     try: 
      from Queue import Queue, Empty 
     except ImportError: 
    #from queue import Queue, Empty # python 3.x 
      print "error" 
     ON_POSIX = 'posix' in sys.builtin_module_names 

     def enqueue_output(out, queue): 
      for line in iter(out.readline, b''): 
       queue.put(line) 
      out.close() 
     p= Popen(["java -Xmx256m -jar bin/HelloWorld.jar"],cwd=r'/home/karen/sphinx4-1.0beta5-src/sphinx4-1.0beta5/',stdout=PIPE, shell=True, bufsize= 4024) 
     q = Queue() 
     t = Thread(target=enqueue_output, args=(p.stdout, q)) 
     print "estoy en el hilo" 
     t.daemon = True # thread dies with the program 
     t.start() 

       print l 

但是,當我執行失敗,出現以下錯誤線程:

Exception in thread Thread-1: 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner 
    self.run() 
    File "/usr/lib/python2.7/site-packages/GNS3/Workspace.py", line 65, in run 
    t = Thread(target=enqueue_output, args=(p.stdout, q)) 
NameError: global name 'Thread' is not defined 

QObject::connect: Cannot queue arguments of type 'QTextCursor' 
(Make sure 'QTextCursor' is registered using qRegisterMetaType().) 

我沒有任何理念!發生什麼事?

回答

2

嘗試改變:

t = Thread(target=enqueue_output, args=(p.stdout, q)) 

到:

t = threading.Thread(target=enqueue_output, args=(p.stdout, q)) 

在當前的命名空間,Thread存在爲threading.Thread(在threading模塊的成員),所以當你說Thread獨自一人,Python可以找不到匹配並拋出錯誤。

+0

謝謝@RocketDonkey我修正了Thread的錯誤!但是仍然出現以下錯誤:'QObject :: connect:無法排隊'QTextCursor'類型的參數 (確保使用qRegisterMetaType()註冊'QTextCursor')'任何想法? – karensantana

+1

@ karensantana很高興聽到它 - 至於第二個錯誤,這看起來像一個QT相關的問題,不幸的是,我從字面上知道它什麼都不知道:)但是我做了一個快速搜索,似乎你需要創建和註冊元類型,並從我看到的代碼示例中,它不是用Python完成的(這只是基於大約5分鐘的研究 - 我可能會錯過某些東西)。不知道這是否(http://doc.qt.digia.com/4.1/qmetatype.html#qRegisterMetaType)對您有意義,但似乎與錯誤有關。 – RocketDonkey

+1

@karensantana另外,如果這樣做不成功,你可以試着問一個新問題 - 這裏有很多聰明的人,有人肯定會成爲Qt專家:)祝你好運! – RocketDonkey