2012-11-30 68 views
26

我正在處理python和即時嘗試執行一個線程,需要1參數「q」,但當我試圖執行它一個奇怪的異常發生時,這是我的代碼:在線程中的異常:必須是一個序列,而不是實例

class Workspace(QMainWindow, Ui_MainWindow): 
    """ This class is for managing the whole GUI `Workspace'. 
     Currently a Workspace is similar to a MainWindow 
    """ 

    def __init__(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 = threading.Thread(target=enqueue_output, args=(p.stdout, q)) 
     #t = Thread(target=enqueue_output, args=(p.stdout, q)) 

     t.daemon = True # thread dies with the program 
     t.start() 

# ... do other things here 
     def myfunc(q): 
      while True: 

       try: line = q.get_nowait() 
     # or q.get(timeout=.1) 
       except Empty: 
        print('') 
       else: # got line 
    # ... do something with line 
        print "No esta null" 
        print line 


     thread = threading.Thread(target=myfunc, args=(q)) 
     thread.start() 

它失敗,出現以下錯誤:

Exception in thread Thread-2: 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner 
    self.run() 
    File "/usr/lib/python2.7/threading.py", line 504, in run 
    self.__target(*self.__args, **self.__kwargs) 
TypeError: myfunc() argument after * must be a sequence, not instance 

我沒有想法發生了什麼事! 請幫忙!

+0

另請參見:http://stackoverflow.com/q/37400133/1240268(對於那些看到此異常,因爲它們的類型尚未定義明星解包)。 –

回答

43

args參數threading.Thread應該是一個數組和你逝去的(q)這是不是 - 它是一樣的q

我想你想要一個元素元組:你應該寫(q,)

+1

謝謝@Tibo!它運作得很好! – karensantana

相關問題