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 = "http://129.192.81.161:5000/"
sleep_time = 0.25
T_end = 10
async def send_request(session, count):
t_send = time.time()
async with session.post(url, 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))
aiohttp==3.6.2
asyncio==3.4.3
flask==1.1.2
gunicorn==20.0.4
\ No newline at end of file
# To start as an FCFS queue, use
# gunicorn --bind 0.0.0.0:5000 server:app --log-level info --workers 1 --worker-class sync
#
# For more gunicorn worker settings, see https://docs.gunicorn.org/en/latest/design.html
import time
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def main():
if request.method == 'POST':
data = request.json
time.sleep(data['sleep-time'])
print(data['sleep-time'])
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__":
app.run(host='0.0.0.0', 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