2012-11-30 13 views
26

Im Arbeiten an Python und ich versuche, einen Thread, der 1 Parameter "q" dauert ausführen, aber wenn ich versuche, es eine seltsame Ausnahme auftritt, hier ist mein Code:Ausnahme im Thread: muss eine Sequenz sein, keine Instanz

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

Es wurde mit folgendem Fehler fehl:

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 

ich nicht Ahnung, was passiert ist! Hilfe bitte!

+0

Siehe auch: http://stackoverflow.com/q/37400133/1240268 (für diejenigen, die diese Ausnahme sehen, weil ihr Typ das Star-Entpacken nicht definiert hat). –

Antwort

43

Der args Parameter threading.Thread sollte ein Tupel sein und Sie sind vorbei (q) was nicht - es ist die gleiche wie q ist.

Ich vermute, Sie wollten ein 1-Element-Tupel: Sie sollten (q,) schreiben.

+1

Danke @Tibo! Es hat sharm funktioniert! – karensantana

Verwandte Themen