Skip to content

Add Mutex for MPD commands #172

@arne123

Description

@arne123

I need thread safety for python-mpd2, therefore I added a method to my class and wrap all calls to mpd over the method below.
This seems to work ok (except that I have trouble to catch the Connection Error, but this is a different story)

Now I wanted to do this a little bit more pythonic.
I was thinking about inheriting the MPDClient class and overwrite either the mpd_commands(object): decorator or the mpd_command_provider(cls): decorator incorporating the mutex below.

Would one of these methods be the be the right approach?

Does one have an other proposal?

    def mpd_retry_with_mutex(self, mpd_cmd, params=None):
        retry = 2
        with self.mpd_mutex:
            while retry:
                try:
                    if params is None:
                        ret = mpd_cmd()
                    else:
                        ret = mpd_cmd(params)
                    break
                except ConnectionError:
                    if retry:
                        retry -= 1
                        self.connect()
                    else:
                        ret = {}
                        break
            return ret

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions