Created
October 6, 2016 09:11
-
-
Save braoru/ac3858e0208ab67967c1da9d192504b5 to your computer and use it in GitHub Desktop.
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/python -i | |
from concurrent import futures | |
import logging | |
import time | |
from os import getpid | |
import datetime | |
from circuits import Component, Event, Debugger, handler, task, Worker | |
from circuits.web import Server, Logger, BaseController, Controller | |
import json | |
# Logging | |
logging.basicConfig( | |
format='%(asctime)s %(name)s %(levelname)s %(process)d %(thread)d %(funcName)s() %(threadName)s %(message)s', | |
datefmt='%m/%d/%Y %I:%M:%S %p' | |
) | |
logger = logging.getLogger("test") | |
logger.setLevel(logging.DEBUG) | |
class work(Event): | |
""" | |
""" | |
class App(Component): | |
def init(self): | |
Server(('0.0.0.0', 11111)).register(self) | |
Logger().register(self) | |
Debugger().register(self) | |
JSONSerializer().register(self) | |
MyController().register(self) | |
def date(): | |
logger.info("In Task") | |
for n in range(1, 20): | |
logger.info(n) | |
logger.info(datetime.datetime.now()) | |
time.sleep(1) | |
return n | |
class JSONSerializer(Component): | |
channel = "web" | |
# 1 higher than the default response handler | |
@handler("response", priority=1.0) | |
def serialize_response_body(self, response): | |
response.headers["Content-Type"] = "application/json" | |
response.body = json.dumps(response.body) | |
class MyController(Controller): | |
channel = "/deployable" | |
_worker = Worker() | |
_executor = concurrent.futures.ThreadPoolExecutor(max_workers=2) | |
def POST(self, *args, **kwargs): | |
data = str(self.request.body.read().decode()) | |
logger.info(data) | |
logger.info("Start Task") | |
x = yield self.call(task(date), self._worker) | |
logger.info(x.value) | |
return { | |
"yoda": "To the hand you talk, because listening i'm not.", | |
"pid": getpid() | |
} | |
if __name__ == "__main__": | |
app = App() | |
app.run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment