Python Klein - Non Blocking Api
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
Solution 1:
So, the problem is your delay
function - its a blocking function, because python's time.sleep
- is blocking. More on this you can read here.
You should use task.deferLater
which you can think of as the non-blocking sleep function of Twisted framework, similar to asyncio.sleep()
# app.pyimport datetime
import json
import treq
from klein import Klein
from twisted.internet import task, reactor
app = Klein()
asyncdefdelay1(secs):
await task.deferLater(reactor, secs)
return"Works"defdelay2(secs):
return task.deferLater(reactor, secs, lambda: "Works")
@app.route('/', branch=True)asyncdefmain(request):
some_data = await delay1(5) # or some_data = await delay2(5) return json.dumps([{
"status": "200",
"time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"data": some_data
}])
defsend_requests():
for i inrange(2):
print("request", datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
d = treq.get('http://localhost:8080')
d.addCallback(treq.content)
d.addCallback(lambda x: print("response", x.decode()))
# send 2 concurrent requests every 10 seconds to test the code
repeating = task.LoopingCall(send_requests)
repeating.start(10, now=False)
app.run("localhost", 8080)
to run the code
$ pip3 install treq klein
$ python3 app.py
wait 10 seconds and the output should be
2019-10-2117:17:11-0400 [-] request 2019-10-2117:17:112019-10-2117:17:11-0400 [-] request 2019-10-2117:17:112019-10-2117:17:16-0400 [-] "127.0.0.1" - - [21/Oct/2019:21:17:16 +0000] "GET / HTTP/1.1"20067"-""-"2019-10-2117:17:16-0400 [-] "127.0.0.1" - - [21/Oct/2019:21:17:16 +0000] "GET / HTTP/1.1"20067"-""-"2019-10-2117:17:16-0400 [-] response [{"status": "200", "time": "2019-10-21 17:17:16", "data": "Works"}]
2019-10-2117:17:16-0400 [-] response [{"status": "200", "time": "2019-10-21 17:17:16", "data": "Works"}]
Post a Comment for "Python Klein - Non Blocking Api"