zzzsochi/aiompd

"yield from" still exists

Closed this issue · 5 comments

Since you've adopted the "async def" syntax, it would be coherent to use the "await" syntax.
"yield from" syntax can is present in the file helpers.py

Yes. I'll rewrite it later.
Or make me a PR. :-)

varac commented

Also, providing an example using asyncio.run() would be helpful.

asyncio.run? It's strange. This is library, not a full program. In example from readme, you can use some about this:

asyncio.run(asyncio.wait([nexter(mpc), volumer(mpc)]))
varac commented

@zzzsochi Thx. Here's the example from the README.md without any yield from and with asyncio.run() (imo much better readable) - although it requires python 3.7:

#!/usr/bin/env python3.7
# -*- coding: utf-8 -*-

import asyncio
import aiompd

URLS = [
    "http://stream.fsk-hh.org:8000/fsk.mp3",
    "http://streaming.fueralle.org:8000/corax.mp3"
]
PLAY_TIME = 15


async def nexter(mpc):
    await mpc.clear()

    for url in URLS:
        await mpc.add(url)

    for n in range(len(URLS)):
        print("Playing track", n)
        await mpc.play(track=n)
        await asyncio.sleep(PLAY_TIME)


async def volumer(mpc):
    timeout = (len(URLS) * PLAY_TIME) / 200

    for volume in range(0, 101, 1):
        await mpc.set_volume(volume)
        await asyncio.sleep(timeout)

    for volume in range(100, -1, -1):
        await mpc.set_volume(volume)
        await asyncio.sleep(timeout)


async def main():
    mpc = await aiompd.Client.make_connection()
    tasks = [nexter(mpc), volumer(mpc)]
    await asyncio.gather(*tasks)

asyncio.run(main())

Updated.