9

有人在 gevent 中使用 PyQt 吗?如何将 PyQt 循环链接到 gevent?

http://www.gevent.org/ - 基于协程的 Python 网络库,它使用 greenlet 在 libevent 事件循环之上提供高级同步 API。

4

5 回答 5

5

您可以使用 Qt IDLE“计时器”来允许gevent处理其微线程,而不会在短时间内处理任何 Qt 事件,例如 10 毫秒。它仍然不完美,因为它没有提供“最平滑”的可能集成。这是因为我们没有为 Qt 和 gevent 使用单个事件循环,只是及时“交错”它们。

正确的解决方案是允许 libevent 以某种方式监听新的 Qt 事件,但我还没有弄清楚如何在实践中做到这一点。当 GUI 事件到达事件队列时,让 Qt 通过套接字向 gevent 发送一些东西可能会有所帮助。有人解决了吗?

工作示例:

""" Qt - gevent event loop integration using a Qt IDLE timer
"""

import sys, itertools

import PySide
from PySide import QtCore, QtGui

import gevent

# Limit the IDLE handler's frequency while still allow for gevent
# to trigger a microthread anytime
IDLE_PERIOD = 0.01

class MainWindow(QtGui.QMainWindow):

    def __init__(self, application):

        QtGui.QMainWindow.__init__(self)

        self.application = application

        self.counter = itertools.count()

        self.resize(400, 100)
        self.setWindowTitle(u'Counting: -')

        self.button = QtGui.QPushButton(self)
        self.button.setText(u'Reset')
        self.button.clicked.connect(self.reset_counter)

        self.show()

    def counter_loop(self):

        while self.isVisible():
            self.setWindowTitle(u'Counting: %d' % self.counter.next())
            gevent.sleep(0.1)

    def reset_counter(self):

        self.counter = itertools.count()

    def run_application(self):

        # IDLE timer: on_idle is called whenever no Qt events left for processing
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.on_idle)
        self.timer.start(0)

        # Start counter
        gevent.spawn(self.counter_loop)

        # Start you application normally, but ensure that you stop the timer
        try:
            self.application.exec_()
        finally:
            self.timer.stop()

    def on_idle(self):

        # Cooperative yield, allow gevent to monitor file handles via libevent
        gevent.sleep(IDLE_PERIOD)

def main():

    application = QtGui.QApplication(sys.argv)
    main_window = MainWindow(application)
    main_window.run_application()

if __name__ == '__main__':
    main()
于 2011-11-27T05:37:21.337 回答
3

我尝试了以下方法:为 gevent 提供“PyQt 后端”,即。使用 QSocketNotifier、QTimer 等 PyQt 构造的 gevent 循环的实现,而不是 libev 循环。最后我发现它比做相反的要容易得多,而且性能非常好(Qt的循环是基于Linux下的glib,还不错)。

以下是 github 上的项目链接,供感兴趣的人参考: https ://github.com/mguijarr/qtgevent

这只是一个开始,但它适用于我所做的测试。如果对 gevent 和 PyQt 有更多经验的人可以做出贡献,我会很高兴。

于 2013-10-01T23:17:05.947 回答
2

以下是如何通过示例的 session1 更改 pyqt 以进行合作:https ://github.com/traviscline/pyqt-by-example/commit/b5d6c61daaa4d2321efe89679b1687e85892460a

于 2011-02-16T00:37:29.717 回答
1

你应该避免使用 app.exec_(),它是一个循环函数,它使用这个函数来处理事件:

http://doc.qt.nokia.com/stable/qcoreapplication.html#processEvents

所以你可以直接调用 processEvents 。

于 2011-01-17T07:36:37.647 回答
1

我发布了一个名为eventlet-pyqt的项目。我希望它对想要在 PyQt 应用程序中使用 greenlet 的人有用。我也尝试过 gevent,但由于我的 C 语言经验不佳,我很难为 libevent 编写插件。QApplicaton::processEvents()使用零间隔的主要问题QTimer是,程序运行到无限循环,导致 100% 的 CPU 内核使用率。为了避免这种情况,我编写了一个新的集线器来select()用 PyQt 的QSocketNotifier. 希望这个消息可以帮助一些人。

于 2013-08-27T14:48:55.073 回答