Commits

David Larlet committed 704255a Merge

Merge work from Graham King, thanks

Comments (0)

Files changed (1)

storages/backends/mosso.py

 
     def seek(self, pos):
         self._pos = pos
+
+
+class ThreadSafeCloudFilesStorage(CloudFilesStorage):
+    """
+    Extends CloudFilesStorage to make it thread safer.
+
+    As long as you don't pass container or cloud objects
+    between threads, you'll be thread safe.
+
+    Uses one cloudfiles connection per thread.
+    """
+
+    def __init__(self, *args, **kwargs):
+        super(ThreadSafeCloudFilesStorage, self).__init__(*args, **kwargs)
+
+        import threading
+        self.local_cache = threading.local()
+
+    def _get_connection(self):
+        if not hasattr(self.local_cache, 'connection'):
+            connection = cloudfiles.get_connection(self.username,
+                                    self.api_key, **self.connection_kwargs)
+            self.local_cache.connection = connection
+
+        return self.local_cache.connection
+
+    connection = property(_get_connection, CloudFilesStorage._set_connection)
+
+    def _get_container(self):
+        if not hasattr(self.local_cache, 'container'):
+            container = self.connection.get_container(self.container_name)
+            self.local_cache.container = container
+
+        return self.local_cache.container
+
+    container = property(_get_container, CloudFilesStorage._set_container)
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.