oauth2client.contrib.multiprocess_file_storage module¶
Multiprocess file credential storage.
This module provides file-based storage that supports multiple credentials and cross-thread and process access.
This module supersedes the functionality previously found in multistore_file.
- This module provides
MultiprocessFileStorage
which: - Is tied to a single credential via a user-specified key. This key can be used to distinguish between multiple users, client ids, and/or scopes.
- Can be safely accessed and refreshed across threads and processes.
- Process & thread safety guarantees the following behavior:
- If one thread or process refreshes a credential, subsequent refreshes from other processes will re-fetch the credentials from the file instead of performing an http request.
- If two processes or threads attempt to refresh concurrently, only one will be able to acquire the lock and refresh, with the deadlock caveat below.
- The interprocess lock will not deadlock, instead, the if a process can
not acquire the interprocess lock within
INTERPROCESS_LOCK_DEADLINE
it will allow refreshing the credential but will not write the updated credential to disk, This logic happens during every lock cycle - if the credentials are refreshed again it will retry locking and writing as normal.
Usage¶
Before using the storage, you need to decide how you want to key the credentials. A few common strategies include:
- If you’re storing credentials for multiple users in a single file, use a unique identifier for each user as the key.
- If you’re storing credentials for multiple client IDs in a single file, use the client ID as the key.
- If you’re storing multiple credentials for one user, use the scopes as the key.
- If you have a complicated setup, use a compound key. For example, you can use a combination of the client ID and scopes as the key.
Create an instance of MultiprocessFileStorage
for each credential you
want to store, for example:
filename = 'credentials'
key = '{}-{}'.format(client_id, user_id)
storage = MultiprocessFileStorage(filename, key)
To store the credentials:
storage.put(credentials)
If you’re going to continue to use the credentials after storing them, be sure
to call set_store()
:
credentials.set_store(storage)
To retrieve the credentials:
storage.get(credentials)
-
oauth2client.contrib.multiprocess_file_storage.
INTERPROCESS_LOCK_DEADLINE
= 1¶ The maximum amount of time, in seconds, to wait when acquire the interprocess lock before falling back to read-only mode.
-
class
oauth2client.contrib.multiprocess_file_storage.
MultiprocessFileStorage
(filename, key)[source]¶ Bases:
oauth2client.client.Storage
Multiprocess file credential storage.
Parameters: - filename – The path to the file where credentials will be stored.
- key – An arbitrary string used to uniquely identify this set of credentials. For example, you may use the user’s ID as the key or a combination of the client ID and user ID.
-
acquire_lock
()[source]¶ Acquires any lock necessary to access this Storage.
This lock is not reentrant.
-
locked_get
()[source]¶ Retrieves the current credentials from the store.
Returns: An instance of oauth2client.client.Credentials
or None.
-
locked_put
(credentials)[source]¶ Writes the given credentials to the store.
Parameters: credentials – an instance of oauth2client.client.Credentials
.