2012-11-30 32 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

相关问题