【问题标题】:Python: exception in ThreadPython:线程中的异常
【发布时间】:2012-11-30 02:42:49
【问题描述】:

我正在尝试在 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().)

我不知道!发生了什么?

【问题讨论】:

    标签: python multithreading ipc


    【解决方案1】:

    尝试改变:

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

    到:

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

    在您当前的命名空间中,Threadthreading.Threadthreading 模块的成员)的形式存在,因此当您单独说 Thread 时,Python 找不到匹配项并引发该错误。

    【讨论】:

    • 谢谢@RocketDonkey 我修复了 Thread 的错误!但是出现以下错误:QObject::connect: Cannot queue arguments of type 'QTextCursor' (Make sure 'QTextCursor' is registered using qRegisterMetaType().)有什么想法吗?
    • @karensantana 很高兴听到它 - 至于第二个错误,这看起来像是一个与 QT 相关的问题,不幸的是我对此一无所知:) 但是我做了一个快速搜索,它出现了您需要创建和注册元类型,并且从我看到的代码示例中,它不是在 Python 中完成的(这只是基于大约 5 分钟的研究——我可能会遗漏一些东西)。不确定这 (doc.qt.digia.com/4.1/qmetatype.html#qRegisterMetaType) 对您是否有意义,但这似乎与错误有关。
    • @karensantana 另外,如果这不起作用,您可以尝试提出一个新问题 - 这里有很多聪明人,而且一定会有人成为 Qt 专家 :) 祝一切顺利!
    • 非常感谢! :) 我会试试的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多