Need help with Klein API for Non-blocking. This is a simple test app:
# -*- coding: utf-8 -*-
import datetime
import json
import time
from klein import Klein
app = Klein()
async def delay(seconds):
"""Set some delay for test"""
time.sleep(seconds)
return "Works"
@app.route('/', branch=True)
async def main(request):
some_data = await delay(5)
return json.dumps([{
"status": "200",
"time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"data": some_data
}])
app.run("localhost", 8080)
Then simply run my server.py
followed by 2 request at the same time to the http://127.0.0.1:8080/
.
The results are:
[ { "status": "200", "time": "2019-10-18 20:57:16", "data": "Works" } ]
[ { "status": "200", "time": "2019-10-18 20:57:21", "data": "Works" } ]
5 seconds delay between each response.
Question:
How make this code working with 2 requests at the same time, now it's working one by one...
Also tried to use twistd, results are the same
PYTHONPATH=. twistd --pidfile=apserver.pid -n web --class=api.resource --port tcp:8000:interface=0.0.0.0
Thanks