Created
February 1, 2018 15:30
-
-
Save asvetlov/0a4e4c40bca14ff797f6e27cfef4220c to your computer and use it in GitHub Desktop.
HTTP keepalive illustration
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 asyncio | |
import aiohttp | |
import random | |
async def test_client(): | |
async with aiohttp.ClientSession() as session: | |
while True: | |
async with session.get('http://127.0.0.1:8080/') as r: | |
result = await r.text() | |
print(result) | |
await asyncio.sleep(random.uniform(0.2, 0.5)) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(test_client()) |
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 asyncio | |
import aiohttp | |
from aiohttp import web | |
async def index(request): | |
return web.Response(body=b'OK') | |
async def main(): | |
loop = asyncio.get_event_loop() | |
app = aiohttp.web.Application() | |
app.router.add_get('/', index) | |
handler = app.make_handler(keepalive_timeout=1) | |
srv = await loop.create_server(handler, '127.0.0.1', 8080) | |
# runner = web.AppRunner(app, handle_signals=True, keepalive_timeout=1) | |
# await runner.setup() | |
# site = web.TCPSite(runner, '127.0.0.1', 8080) | |
# await site.start() | |
fut = loop.create_future() | |
await fut | |
asyncio.get_event_loop().run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment