Bạn có thể sử dụng một IDLE Qt "hẹn giờ" để cho phép gevent
chế biến microthreads của nó trong khi có sự kiện nào Qt xử lý cho một khoảng thời gian ngắn, cho ví dụ 10 mili giây. Nó vẫn chưa hoàn hảo, vì nó không tạo ra sự tích hợp tốt nhất có thể. Đó là bởi vì chúng tôi không sử dụng một vòng lặp sự kiện duy nhất cho cả Qt và gevent, chỉ cần "xen kẽ" chúng trong thời gian.
Giải pháp chính xác sẽ cho phép không muốn nghe các sự kiện Qt mới bằng cách nào đó, nhưng tôi chưa thể tìm ra cách thực hiện điều đó trong thực tế. Có thể có Qt để gửi một cái gì đó để gevent thông qua một ổ cắm khi một sự kiện GUI đến hàng đợi sự kiện sẽ giúp đỡ. Có ai đã giải quyết điều đó không?
dụ làm việc:
""" 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()
"Gevent" là gì? Vui lòng thêm liên kết cho câu hỏi của bạn. –
http://www.gevent.org/ - thư viện mạng Python dựa trên coroutine sử dụng greenlet để cung cấp API đồng bộ cao cấp ở đầu vòng lặp sự kiện không thích hợp. –
rất thú vị .. bạn sẽ làm gì khi pyqt + gevent hoạt động? – linjunhalida