Created
November 11, 2011 18:30
-
-
Save andycodes00/1358794 to your computer and use it in GitHub Desktop.
Greenlet based PyQt and Flask application
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
#!/usr/bin/env python | |
import sys | |
import time | |
from flask import Flask | |
from PyQt4 import QtCore, QtGui, QtWebKit | |
import gevent.wsgi | |
class WebView(QtWebKit.QWebView): | |
def __init__(self): | |
QtWebKit.QWebView.__init__(self) | |
self.load(QtCore.QUrl("http://localhost:5000/")) | |
self.connect(self , QtCore.SIGNAL("clicked()"), self.closeEvent) | |
def closeEvent(self, event): | |
self.deleteLater() | |
app.quit() | |
print "closing gui" | |
g.kill(gevent.GreenletExit, block=False) | |
f.kill(gevent.GreenletExit, block=False) | |
print "are gui and webserver alive? ", g.dead, f.dead | |
class PyQtGreenlet(gevent.Greenlet): | |
def __init__(self, app): | |
gevent.Greenlet.__init__(self) | |
self.app = app | |
def _run(self): | |
while True: | |
self.app.processEvents() | |
while self.app.hasPendingEvents(): | |
self.app.processEvents() | |
gevent.sleep(0.01) | |
gevent.sleep(0.1) | |
if __name__ == "__main__": | |
fapp = Flask(__name__) | |
fapp.debug=True | |
@fapp.route("/") | |
def hello(): | |
print time.time() | |
return '<a href="http://localhost:5000/bye" > quit() </a></br><a href="http://localhost:5000/" > reload </a></br>count={0}'.format(time.time()) | |
@fapp.route("/bye", methods=['GET']) | |
def goodbye(): | |
print "killing webserver" | |
f.kill(block=False) | |
print "is webserver alive? ", f.dead | |
window.closeEvent(QtCore.SIGNAL("clicked()")) | |
return 'goodbye' | |
http_server = gevent.wsgi.WSGIServer(('', 5000), fapp) | |
f = gevent.spawn( http_server.serve_forever) | |
app = QtGui.QApplication(sys.argv) | |
window = WebView() | |
window.show() | |
g = PyQtGreenlet.spawn(app) | |
gevent.joinall([f, g]) | |
Super crappy n00b question, what is this counting exactly? http reqs on a particular server?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you!