Created
February 18, 2014 13:00
-
-
Save aodag/9070531 to your computer and use it in GitHub Desktop.
asyncio + aiohttpでwsgiアプリケーションを実行。せっかくなのでexecutorを使ってコンソール入力をレスポンスで表示とか。python3.4.0b1
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 logging | |
import asyncio | |
from aiohttp.wsgi import WSGIServerHttpProtocol | |
logger = logging.getLogger(__name__) | |
def index(environ, start_response): | |
logger.info("GET") | |
start_response("200 OK", | |
[('Content-type', 'text/plain')]) | |
future = asyncio.Future() | |
loop.run_in_executor(None, get_message, future) | |
message = yield from future | |
return [message.encode("utf-8")] | |
def get_message(future): | |
message = input("message: ") | |
future.set_result(message) | |
class AppHttpProtocol(WSGIServerHttpProtocol): | |
def __init__(self): | |
super(AppHttpProtocol, self).__init__(index) | |
loop = asyncio.get_event_loop() | |
conn = loop.create_server(AppHttpProtocol, "127.0.0.1", port=8000) | |
server = loop.run_until_complete(conn) | |
loop.run_forever() | |
loop.close() |
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
aiohttp==0.6.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment