Created
July 28, 2011 00:55
-
-
Save Evgenus/1110700 to your computer and use it in GitHub Desktop.
Reactors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
class Reactor(object): | |
timeout = 0.01 | |
def __init__(self, callback): | |
self.callback = callback | |
def run(self): | |
raise NotImplementedError() | |
# ____ GTK+ | |
import pygtk | |
pygtk.require('2.0') | |
import gtk | |
import gobject | |
class GtkReactor(Reactor): | |
def run(self): | |
while True: | |
while gtk.events_pending(): | |
gtk.main_iteration(False) | |
if not self.callback(): | |
time.sleep(self.timeout) | |
# ____ Qt | |
from PyQt4 import QtGui | |
class QtReactor(Reactor): | |
def __init__(self, *args, **kwargs): | |
super(QtReactor, self).__init__(*args, **kwargs) | |
self.app = QtGui.QApplication(sys.argv) | |
def run(self): | |
while True: | |
self.app.processEvents() | |
if not self.callback(): | |
time.sleep(self.timeout) | |
def func(): | |
print 1 | |
if __name__ == "__main__": | |
GtkReactor(func).run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment