0

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

我不知道!怎么了?

4

1 回答 1

2

尝试改变:

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

到:

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

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

于 2012-11-30T02:51:31.343 回答