klein
klein copied to clipboard
Combining Klein with other asynchronous Python libraries
Dear Twisted Klein-Team!
thank you for making Klein available! It has the charming interface of Flask but keeps everything asynchronous.
We would like to combine Klein with a python library for bus communication. The following code works fine for us without Klein.
"""Example for switching a light on and off."""
import asyncio
from xknx import XKNX
from xknx.devices import Light
async def main():
"""Connect to KNX/IP bus, switch on light, wait 2 seconds and switch of off again."""
xknx = XKNX()
light = Light(xknx,
name='VarBool',
group_address_switch='2/1/1')
await xknx.start()
i = 0
while i<5:
i = i + 1
print ("Turn on, wait 1 s")
await light.set_on()
await asyncio.sleep(1)
print ("Turn of wait 1s")
await light.set_off()
await asyncio.sleep(1)
await xknx.stop()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
This is our attempt to integrate xknx and Klein:
import treq
from klein import Klein
from xknx import XKNX
from xknx.devices import Light
import asyncio
app = Klein()
@app.route('/on', [ 'GET' ])
async def on(request):
xknx = XKNX()
light = Light(xknx,
name='VarBool',
group_address_switch='2/1/1')
await xknx.start()
await light.set_on()
await xknx.stop()
return 'Light is on'
@app.route('/off', branch=True)
def off(request):
xknx = XKNX()
light = Light(xknx,
name='VarBool',
group_address_switch='2/1/1')
await xknx.start()
await light.set_off()
await xknx.stop()
return 'Light is off'
app.run("localhost", 8080)
Unfortunately this does not work. We tried different things but did not come up with a solution. We usually get an error "error.BuiltIn - No yield from provided for Future".
It would be great if you might suggest how to integrate other asynchronous python code.
Thanks
Georg
We got some feedback from the xknx library developers (see github issue) and it would be great to access the event loop (asyncio.get_event_loop()) of the Klein app. Is there any chance to retrieve it ?
The loop object would be given as an argument.
from xknx import XKNX
from xknx.devices import Light
light = Light(xknx,
loop = klein_loop,
name='VarBool',
group_address_switch='2/1/1')
@GeorgFerdinandSchneider at the moment, getting twisted (which is the framework klein uses) and asyncio (which xknx uses) is a bit weird. To integrate the frameworks, you can run:
import asyncio
from twisted.internet import asyncioreactor
loop = asyncio.get_event_loop()
asyncio.set_event_loop(loop)
asyncioreactor.install(eventloop=loop)
# klein and xknx code here. DONT START asyncio event loop
klein_app.run('0.0.0.0', 8000)
@wsanchez any benefit adding combining asyncio
with twisted
or klein
to the documentation? If not I'd close this issue
Given that this is not entirely trivial given the state of twisted/asyncio interop I'd definitely want to leave this open.
Hello everyone,
apologies for the delay in answering.
Combining the answers provided we where able to develop the following code which so far looks good as we are able to execute the code. We are not completly clear why exactly this code works but more found the solution through try and error.
Unfortunately, we have not been able to test the code with a real KNX device but will update when this is done.
import asyncio
from xknx import XKNX
from xknx.devices import Light
from klein import Klein
loop = asyncio.get_event_loop()
asyncio.set_event_loop(loop)
app = Klein()
@app.route('/on')
async def on(request):
xknx = XKNX()
light = Light(xknx,
name='VarBool',
group_address_switch='2/1/1')
xknx.start()
light.set_on()
xknx.stop()
return 'Light is on'
@app.route('/off')
async def off(request):
xknx = XKNX()
light = Light(xknx,
name='VarBool',
group_address_switch='2/1/1')
xknx.start()
light.set_off()
xknx.stop()
return 'Light is off'
app.run("localhost", 8080)
@GeorgFerdinandSchneider
You might find this mailing list message interesting:
https://twistedmatrix.com/pipermail/twisted-python/2019-April/032280.html