Mic92 / python-mpd2

Python library which provides a client interface for the Music Player Daemon.
GNU Lesser General Public License v3.0
352 stars 119 forks source link

asyncio: Protect connection against request cancellation #154

Closed chrysn closed 3 years ago

chrysn commented 3 years ago

When a user requests a result, it is now pulled out to completion regardless (as the pipe needs to be cleared of it) but does not raise an error any more.

Chunked binary data is handled implicitly: the cancelled coroutines just doesn't request any more chunks.

Closes: https://github.com/Mic92/python-mpd2/issues/148


Test script that illustrates how things are now better than before:

import asyncio

from mpd.asyncio import MPDClient
import random

async def main():
    print("Create MPD client")
    client = MPDClient()
    try:
        await client.connect('localhost', 6600)
    except Exception as e:
        print("Connection failed:", e)
        return

    print("Connected to MPD version", client.mpd_version)

    while True:
        try:
            status = await client.status()
        except Exception as e:
            print("Status error:", e)
            return
        else:
            print("Status success:", status)

        await client.seekid(int(status['songid']), random.random() * 10)

        semiseek = client.seekid(int(status['songid']), random.random() * 10)
        semiseek.cancel()

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())