twisted / twisted/klein

Combining Klein with other asynchronous Python libraries

Open
#234 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation
Dominant language
Python
Stars
838
Forks
123
Avg merge
7h 58m
Merged PRs (30d)
12

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the standalone xknx asyncio example and the Klein /on and /off route examples, then reproduce the “No yield from provided for Future” error. No repository files or tests are named; done would be a documented, verified integration path or a clearly reproducible project change showing both routes can switch the light successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.