-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
File caching for the oauth2client >= 4.0.0 #325
Comments
Sure, mostly what needs to happen is that the current file cache needs to
be updated to not use oauth2client's locked file.
…On Sun, Jan 8, 2017, 2:14 PM Ondrej Medek ***@***.***> wrote:
@tmatsuo <https://github.com/tmatsuo> proposed in #110
<#110> to cache
discovery.build(), however, #294
<#294> has stopped
using cache outside of the GAE (appengine) environment (see also #299
<#299>). E.g.
the discovery.build() is not cached in the CGE environment. Is it
possible to bring the file caching back, or provide some example of file
cache (subclass of googleapiclient.discovery_cache.Cache)?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#325>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAPUc2WtGDCjrz4H1QhftQxypDuOidnpks5rQV-6gaJpZM4Ld0fj>
.
|
Is there a known workaround in the meantime? |
I created a small workaround by using a in memory cache (see example below). Does anyone know if there should be a regular cache invalidation? Currently the memory cache is cleared on each deployment, I hope that's ok for the time being. In memory cache:
Usage:
|
AFAIK the Google production API should not change (the same version of API), so the you do not need to invalidate the cache. However, the +1 for a neat, simple If you want to share the cache among processes, (like running scripts by the cron,) you may copy the old good oauth2client |
@jonparrott what about to change file lock to the |
I think it's likely possible to do this without any sort of locking.
…On Wed, May 17, 2017, 12:02 AM Ondrej Medek ***@***.***> wrote:
@jonparrott <https://github.com/jonparrott> what about to change file
lock to the multiprocessing.RLock() in the original locked_file.py? Would
be it sufficient solution to accept back to the repo?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#325 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAPUc-ne-ZS8p4rfQbV1qs3RtjXgc7Opks5r6puOgaJpZM4Ld0fj>
.
|
You mean a file cache, shared among multiple processes, without any locking? How to prevent data corruption, when 2 process write to the same file? |
Likely using a folder containing a file for each cached discovery doc. No
locking needed. Worst case scenario two processes write the same file twice.
…On Wed, May 17, 2017, 12:08 AM Ondrej Medek ***@***.***> wrote:
You mean a file cache, shared among multiple processes, without any
locking? How to prevent data corruption, when 2 process write to the same
file?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#325 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAPUc9PWEHEBR_dWKW4KmNhmgV4lH8qiks5r6pzbgaJpZM4Ld0fj>
.
|
FYI to use the locked_file from oauth2client 3.0.0 without that package (it's not required since 1.7.3), just use the gist https://gist.github.com/xmedeko/e5293707e075d18c9dde8a574d2670e5 and call the |
Based on Schweigi's solution, I wrote a simple file based cache (unix-only) to also cache across processes. import os.path
import hashlib
import tempfile
class DiscoveryCache:
def filename(self, url):
return os.path.join(
tempfile.gettempdir(),
'google_api_discovery_' + hashlib.md5(url.encode()).hexdigest())
def get(self, url):
try:
with open(self.filename(url), 'rb') as f:
return f.read().decode()
except FileNotFoundError:
return None
def set(self, url, content):
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(content.encode())
f.flush()
os.fsync(f)
os.rename(f.name, self.filename(url)) |
I'm opting to close this as it's unlikely we will implement it in the future.
|
"Not errors, just warnings" ... .googleapis/google-api-python-client#325
to suppress errors in logs: file_cache is unavailable when using oauth2client >= 4.0.0 googleapis/google-api-python-client#299 googleapis/google-api-python-client#325
I don't know if this changed at some point, but I had to import |
The original main.py creates noisy log messages when the function is run file_cache is unavailable when using oauth2client >= 4.0.0: ``` 2020-06-26T19:34:03.459Z datastore_export file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth E datastore_export 2020-06-26T19:34:03.459Z datastore_export Traceback (most recent call last): E datastore_export 2020-06-26T19:34:03.459Z datastore_export File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module> E datastore_export 2020-06-26T19:34:03.459Z datastore_export from oauth2client.contrib.locked_file import LockedFile E datastore_export 2020-06-26T19:34:03.459Z datastore_export ModuleNotFoundError: No module named 'oauth2client' E datastore_export 2020-06-26T19:34:03.459Z datastore_export E datastore_export 2020-06-26T19:34:03.459Z datastore_export During handling of the above exception, another exception occurred: E datastore_export 2020-06-26T19:34:03.459Z datastore_export E datastore_export 2020-06-26T19:34:03.459Z datastore_export Traceback (most recent call last): E datastore_export 2020-06-26T19:34:03.459Z datastore_export File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 37, in <module> E datastore_export 2020-06-26T19:34:03.459Z datastore_export from oauth2client.locked_file import LockedFile E datastore_export 2020-06-26T19:34:03.459Z datastore_export ModuleNotFoundError: No module named 'oauth2client' E datastore_export 2020-06-26T19:34:03.459Z datastore_export E datastore_export 2020-06-26T19:34:03.459Z datastore_export During handling of the above exception, another exception occurred: E datastore_export 2020-06-26T19:34:03.459Z datastore_export E datastore_export 2020-06-26T19:34:03.459Z datastore_export Traceback (most recent call last): E datastore_export 2020-06-26T19:34:03.459Z datastore_export File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/__init__.py", line 44, in autodetect E datastore_export 2020-06-26T19:34:03.459Z datastore_export from . import file_cache E datastore_export 2020-06-26T19:34:03.459Z datastore_export File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 41, in <module> E datastore_export 2020-06-26T19:34:03.459Z datastore_export "file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth" E datastore_export 2020-06-26T19:34:03.459Z datastore_export ImportError: file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth E datastore_export ``` While they don't interfere with the function's operation, they are disconcerting and not necessary. This implements the workaround suggested in googleapis/google-api-python-client#325 (which creates an in-memory cache object to use in replacement).
* fix: Replace default file_cache usage with simple memory cache The original main.py creates noisy log messages when the function is run file_cache is unavailable when using oauth2client >= 4.0.0: ``` 2020-06-26T19:34:03.459Z datastore_export file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth E datastore_export 2020-06-26T19:34:03.459Z datastore_export Traceback (most recent call last): E datastore_export 2020-06-26T19:34:03.459Z datastore_export File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module> E datastore_export 2020-06-26T19:34:03.459Z datastore_export from oauth2client.contrib.locked_file import LockedFile E datastore_export 2020-06-26T19:34:03.459Z datastore_export ModuleNotFoundError: No module named 'oauth2client' E datastore_export 2020-06-26T19:34:03.459Z datastore_export E datastore_export 2020-06-26T19:34:03.459Z datastore_export During handling of the above exception, another exception occurred: E datastore_export 2020-06-26T19:34:03.459Z datastore_export E datastore_export 2020-06-26T19:34:03.459Z datastore_export Traceback (most recent call last): E datastore_export 2020-06-26T19:34:03.459Z datastore_export File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 37, in <module> E datastore_export 2020-06-26T19:34:03.459Z datastore_export from oauth2client.locked_file import LockedFile E datastore_export 2020-06-26T19:34:03.459Z datastore_export ModuleNotFoundError: No module named 'oauth2client' E datastore_export 2020-06-26T19:34:03.459Z datastore_export E datastore_export 2020-06-26T19:34:03.459Z datastore_export During handling of the above exception, another exception occurred: E datastore_export 2020-06-26T19:34:03.459Z datastore_export E datastore_export 2020-06-26T19:34:03.459Z datastore_export Traceback (most recent call last): E datastore_export 2020-06-26T19:34:03.459Z datastore_export File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/__init__.py", line 44, in autodetect E datastore_export 2020-06-26T19:34:03.459Z datastore_export from . import file_cache E datastore_export 2020-06-26T19:34:03.459Z datastore_export File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 41, in <module> E datastore_export 2020-06-26T19:34:03.459Z datastore_export "file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth" E datastore_export 2020-06-26T19:34:03.459Z datastore_export ImportError: file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth E datastore_export ``` While they don't interfere with the function's operation, they are disconcerting and not necessary. This implements the workaround suggested in googleapis/google-api-python-client#325 (which creates an in-memory cache object to use in replacement). * Add explanatory comment Add a comment explaining the inclusion of MemoryCache instance in the `build` invocation. * Update main.py PEP8-mandated two newlines between top-level elements Co-authored-by: Christopher Wilcox <crwilcox@google.com> Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
An equivalent & simpler workaround is to set discovery.build(api, version, http=http, cache_discovery=False) Since according to the source the "cache" is never reused in the "single, long running process" case. |
…ent#325) Signed-off-by: hldh214 <hldh214@gmail.com>
|
@tmatsuo proposed in #110 to cache
discovery.build()
, however, #294 has stopped using cache outside of the GAE (appengine) environment (see also #299). E.g. thediscovery.build()
is not cached in the CGE environment. Is it possible to bring the file caching back, or provide some example of file cache (subclass ofgoogleapiclient.discovery_cache.Cache
)?The text was updated successfully, but these errors were encountered: