线程中的异常:必须是序列,而不是实例。

27

我正在使用Python工作,尝试执行一个带有一个参数“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

我不知道发生了什么事情!请帮忙!

(这段话是关于一个人对技术问题的求助)

参见:https://dev59.com/fZffa4cB1Zd3GeqP6VaR(对于那些因为类型未定义星号解包而看到这个异常的人)。 - Andy Hayden
1个回答

44

threading.Thread 中的 args 参数应该是一个元组,而你正在传递 (q),但它不是 - 它与 q 相同。

我猜你想要一个只有一个元素的元组: 你应该这样写: (q,)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接