Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mutex for MPD commands #172

Open
arne123 opened this issue May 31, 2021 · 0 comments
Open

Add Mutex for MPD commands #172

arne123 opened this issue May 31, 2021 · 0 comments

Comments

@arne123
Copy link

arne123 commented May 31, 2021

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant