Commit bdde7cd0 authored by Johan Ruuskanen's avatar Johan Ruuskanen
Browse files

Add flask example with an asynchronous load generator

parent 82de7de1
import aiohttp
import asyncio
import time
url = ""
sleep_time = 0.25
T_end = 10
async def send_request(session, count):
t_send = time.time()
async with, json={'sleep-time':0.5}) as response:
data = await response.json()
t_resp = time.time()
print("{}, resp: {}, rt: {:10.4f}".format(count, \
data['sleep-time-return'], t_resp - t_send))
async def main(loop):
async with aiohttp.ClientSession() as session:
tasks = set()
t0 = time.time()
t_prev= t0
t = 0
count = 0
while t < T_end:
dt = time.time() - t_prev
waittime = max(sleep_time - dt, 0)
await asyncio.sleep(waittime)
t_prev = time.time()
tasks.add(loop.create_task(send_request(session, count)))
count += 1
t = time.time() - t0
await asyncio.wait(tasks)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
tasks = loop.run_until_complete(main(loop))
\ No newline at end of file
# To start as an FCFS queue, use
# gunicorn --bind server:app --log-level info --workers 1 --worker-class sync
# For more gunicorn worker settings, see
import time
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def main():
if request.method == 'POST':
data = request.json
return {'sleep-time-return': 2*data['sleep-time']}
return '''
<!doctype html>
<title>A simple flask server</title>
<h1>A simple flask server</h1>
if __name__ == "__main__":'', port=5000)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment