Created
April 21, 2015 10:45
-
-
Save czardoz/2e5bf13a233ae28fda9f to your computer and use it in GitHub Desktop.
Simple Flask app with a background task using gevent
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 gevent | |
import gevent.monkey | |
gevent.monkey.patch_all() | |
from gevent.pywsgi import WSGIServer | |
from flask import Flask | |
app = Flask(__name__) | |
app.debug = True | |
# Simple catch-all server | |
@app.route('/', defaults={'path': ''}, methods=['GET', 'POST']) | |
@app.route('/<path:path>', methods=['GET', 'POST']) | |
def catch_all(path): | |
return 'It is Working!' | |
def background(): | |
count = 0 | |
while True: | |
print count | |
count += 1 | |
gevent.sleep(1) | |
if __name__ == '__main__': | |
http_server = WSGIServer(('', 4430), app, keyfile='server.key', certfile='server.crt') | |
srv_greenlet = gevent.spawn(http_server.start) | |
background_task = gevent.spawn(background) | |
try: | |
gevent.joinall([srv_greenlet, background_task]) | |
except KeyboardInterrupt: | |
print "Exiting" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment