Puede utilizar un IDLE "temporizador" Qt para permitir gevent
para el procesamiento de sus microfiletes mientras hay eventos Qt manejados por un corto período de tiempo, por ejemplo 10 milisegundos. Todavía no es perfecto, ya que no brinda la integración más "suave" posible. Es porque no usamos un solo ciclo de eventos para Qt y gevent, simplemente "intercalando" a tiempo.
La solución correcta sería permitir que libevent escuche los nuevos eventos de Qt de alguna manera, pero todavía no he podido descifrar cómo hacerlo en la práctica. Tal vez tener Qt para enviar algo a gevent a través de un socket cuando un evento GUI llegue a la cola del evento ayudaría. ¿Alguien ha resuelto eso?
Ejemplo de trabajo:
""" 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()
¿Qué es "gevent"? Por favor agregue enlaces a su pregunta. –
http://www.gevent.org/ - biblioteca de redes de Python basada en coroutine que usa greenlet para proporcionar una API síncrona de alto nivel en la parte superior del ciclo de evento libevent. –
muy interesante ... ¿qué harás cuando pyqt + gevent funcione? – linjunhalida