[Pkg-mpd-commits] [python-mpd] 154/262: add examples for logging/locking

Simon McVittie smcv at debian.org
Sun May 22 18:16:40 UTC 2016


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch upstream
in repository python-mpd.

commit 3ca8ea7997c5bb90d8dda2af3faf8490a00918a5
Author: Jörg Thalheim <joerg at higgsboson.tk>
Date:   Thu Nov 29 13:13:08 2012 +0100

    add examples for logging/locking
---
 README.md           | 11 ++++++-----
 examples/locking.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 examples/logger.py  |  5 +++++
 3 files changed, 60 insertions(+), 5 deletions(-)

diff --git a/README.md b/README.md
index 3b6a6b4..9c5aca4 100644
--- a/README.md
+++ b/README.md
@@ -203,13 +203,14 @@ self.client.add_command("get_cover", fetch_cover)
 self.client.remove_command("get_cover")
 ```
 
-Known Issues
+Thread-Safety
 ------------
 
-Currently python-mpd is **NOT** thread-safe.
-If you need to access the library from multiple threads,
-you have to either use [locks](http://docs.python.org/library/threading.html#lock-objects)
-or use one mpd client per thread.
+Currently MPDClient is **NOT** thread-safe.
+As it use a socket internal, only one thread can send or receive at the time.
+
+But MPDClient can be easily extended to be thread-safe using [locks](http://docs.python.org/library/threading.html#lock-objects).
+Take a look at examples/locking.py for further informations.
 
 Testing
 -------
diff --git a/examples/locking.py b/examples/locking.py
new file mode 100644
index 0000000..d6bb916
--- /dev/null
+++ b/examples/locking.py
@@ -0,0 +1,49 @@
+from threading import Lock, Thread
+from random import choice
+from mpd import MPDClient
+
+class LockableMPDClient(MPDClient):
+    def __init__(self, use_unicode=False):
+        super(LockableMPDClient, self).__init__()
+        self.use_unicode = use_unicode
+        self._lock = Lock()
+    def acquire(self):
+        self._lock.acquire()
+    def release(self):
+        self._lock.release()
+    def __enter__(self):
+        self.acquire()
+    def __exit__(self, type, value, traceback):
+        self.release()
+
+client = LockableMPDClient()
+client.connect("localhost", 6600)
+# now whenever you need thread-safe access
+# use the 'with' statement like this:
+with client: # acquire lock
+    status = client.status()
+# if you leave the block, the lock is released
+# it is recommend to leave it soon,
+# otherwise your other threads will blocked.
+
+# Let's test if it works ....
+def fetch_playlist():
+    for i in range(10):
+        if choice([0, 1]) == 0:
+            with client:
+                song = client.currentsong()
+            assert isinstance(song, dict)
+        else:
+            with client:
+                playlist = client.playlist()
+            assert isinstance(playlist, list)
+
+threads = []
+for i in range(5):
+    t = Thread(target=fetch_playlist)
+    threads.append(t)
+    t.start()
+for t in threads:
+    t.join()
+
+print("Done...")
diff --git a/examples/logger.py b/examples/logger.py
new file mode 100644
index 0000000..ee48fe8
--- /dev/null
+++ b/examples/logger.py
@@ -0,0 +1,5 @@
+import logging, mpd
+logging.basicConfig(level=logging.DEBUG)
+client = mpd.MPDClient()
+client.connect("localhost", 6600)
+client.find("any", "house")

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mpd/python-mpd.git



More information about the Pkg-mpd-commits mailing list