Python&PyGTK:将数据传输到线程 [英] Python&PyGTK: Transport data to threading

查看:76
本文介绍了Python&PyGTK:将数据传输到线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据传输到线程类,但我不知道有什么问题.下面的代码来自这个问题,我对其进行了一些更改.

I would like to transfer data to threading class, but I can't get what is wrong. The code below is from this question and I changed it a little.

这是一个代码:

import gtk, gobject, threading, time

gobject.threads_init()

class T(threading.Thread):
    pause = threading.Event()
    stop = False

    def start(self, data, *args):
        super(T, self).start()

    def run(self, data):
        while not self.stop:
            self.pause.wait()
            gobject.idle_add(lambda *a : self.rungui(data))
            time.sleep(0.1)

    def rungui(self, data):
        print "printed"
        print data

thread = T()

class Start:

        def toggle_thread(self, data=None, *args):
                if not thread.is_alive():
                    thread.start(data)
                    thread.pause.set()
                    self.button.set_label('Pause Thread')
                    return

                if thread.pause.is_set():
                    thread.pause.clear()
                    self.button.set_label('Resume Thread')
                else:
                    thread.pause.set()
                    self.button.set_label('Pause Thread')

        def __init__(self):
                thread = T()
                window = gtk.Window()
                self.button = gtk.ToggleButton('Start Thread')
                data = 3
                self.button.connect('toggled', lambda *a : self.start(data), None)
                window.add(self.button)
                self.button.show()
                window.show()

        def start(self, data=None):
                self.toggle_thread(data)

        def main(self):
                gtk.main()

if __name__ == "__main__":
        start = Start()
        start.main()

我需要纠正什么才能使线程完全正常工作?

What do I have to correct to get threading fully working?

推荐答案

不要在 gui 线程之外使用 gtk.关于:

Don`t work with gtk out of gui thread. That about:

gobject.idle_add(self.rungui)

链接中的示例工作正常,但需要系统终止命令才能终止.而且 super() 不能给 run() 函数带参数.

Example at your link work fine, but need system kill command for termination. And super() can`t bring arguments to run() function.

我的线程初始化看起来像这样:

My threads initialization looks like this:

class VirtService(threading.Thread):
        def __init__(self, queue):
                threading.Thread.__init__(self)
                self.queue = queue

        def thread_loop(self):
                while self.queue.qsize():
                        data_command = self.queue_get()

...

queue = Queue()

if __name__ == '__main__':
        gobject.threads_init()
        vs = VirtService(queue)

您可以使用 Queue 进行双向数据转换.您也可以使用队列作为命令.在非图形线程中通过 Queue.qet() 创建 C++ poll() 模拟,在 gui 线程中 queue.put()

And you may use Queue for data translation to both directions. You may use also queue for command. In non-graphical thread create c++ poll() analog through Queue.qet(), and in gui thread queue.put()

这篇关于Python&PyGTK:将数据传输到线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆