Created
December 18, 2017 21:52
-
-
Save wware/2651b4788a5c85aaaeccf56ed68f835e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 logging | |
from flask import Flask | |
from twisted.internet import ssl, reactor | |
from twisted.web.wsgi import WSGIResource | |
from twisted.web.server import Site | |
from twisted.python.threadpool import ThreadPool | |
from twisted.application import service | |
from multiprocessing import Event | |
app = Flask("my cool service") | |
@app.route('/', defaults={'path': ''}) | |
@app.route('/<path:path>') | |
def catch_all(path): | |
return 'You want path: %s' % path | |
def do_something(signal): | |
logging.info("Doing something") | |
class ThreadPoolService(service.Service): | |
def __init__(self, pool): | |
self.pool = pool | |
self.exit_signal = Event() | |
def startService(self): | |
service.Service.startService(self) | |
self.pool.start() | |
#use one thread to do something | |
self.exit_signal.clear() | |
self.pool.callInThread(do_something, self.exit_signal) | |
def stopService(self): | |
self.exit_signal.set() | |
self.pool.stop() | |
service.Service.stopService(self) | |
application = service.Application('my cool service') | |
tps = ThreadPoolService(ThreadPool()) | |
tps.setServiceParent(application) | |
wsgiAppAsResource = WSGIResource(reactor, tps.pool, app) | |
reactor.listenSSL( | |
8443, | |
Site(wsgiAppAsResource), | |
ssl.DefaultOpenSSLContextFactory('blah.key', 'blah.crt') | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment