google-app-enginememcachedapp-engine-patchpython-memcached

Memcache not present in App-Engine-Patch?


I'm trying to store a date into memcache using the following code:

from datetime import date
from google.appengine.api.memcache import Client

MEMCACHE_DATE_KEY = 'date'

client = Client()

def last_date():
    return client.get(MEMCACHE_DATE_KEY)

def new_date():
    client.set(MEMCACHE_DATE_KEY, date.today())

I am getting this error:

Traceback (most recent call last):
  File "manage.py", line 4, in 
    setup_env(manage_py_env=True)
  File "/Users/benji/Projects/app-engine-patch-sample/common/appenginepatch/aecmd.py", line 67, in setup_env
    patch_all()
  File "/Users/benji/Projects/app-engine-patch-sample/common/appenginepatch/appenginepatcher/patch.py", line 29, in patch_all
    patch_app_engine()
  File "/Users/benji/Projects/app-engine-patch-sample/common/appenginepatch/appenginepatcher/patch.py", line 520, in patch_app_engine
    db.Model._meta = _meta(db.Model, ())
  File "/Users/benji/Projects/app-engine-patch-sample/common/appenginepatch/appenginepatcher/patch.py", line 258, in __init__
    settings.INSTALLED_APPS
  File "/Users/benji/Projects/share-renting-engine/common/zip-packages/django-1.1.zip/django/utils/functional.py", line 269, in __getattr__
  File "/Users/benji/Projects/share-renting-engine/common/zip-packages/django-1.1.zip/django/conf/__init__.py", line 40, in _setup

  File "/Users/benji/Projects/share-renting-engine/common/zip-packages/django-1.1.zip/django/conf/__init__.py", line 73, in __init__

  File "/Users/benji/Projects/share-renting-engine/common/zip-packages/django-1.1.zip/django/utils/importlib.py", line 35, in import_module
  File "/Users/benji/Projects/share-renting-engine/settings.py", line 120, in 
    from ragendja.settings_post import *
  File "/Users/benji/Projects/app-engine-patch-sample/common/appenginepatch/ragendja/settings_post.py", line 98, in 
    check_app_imports(app)
  File "/Users/benji/Projects/app-engine-patch-sample/common/appenginepatch/ragendja/settings_post.py", line 63, in check_app_imports
    __import__(app, {}, {}, [''])
  File "/Users/benji/Projects/share-renting-engine/engine/__init__.py", line 5, in 
    if date.today() != last_date():
  File "/Users/benji/Projects/share-renting-engine/engine/utils/date.py", line 12, in last_date
    return client.get(MEMCACHE_DATE_KEY)
  File "/Users/benji/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/memcache/__init__.py", line 428, in get
    self._make_sync_call('memcache', 'Get', request, response)
  File "/Users/benji/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 86, in MakeSyncCall
    return stubmap.MakeSyncCall(service, call, request, response)
  File "/Users/benji/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 279, in MakeSyncCall
    assert stub, 'No api proxy found for service "%s"' % service
AssertionError: No api proxy found for service "memcache"

How can i use memcache with app-engine-patch?

Thanks for your time.


Solution

  • It looks like you're attempting to make a memcache call at import time. Judging from the stacktrace, Django imports your modules before it sets up the App Engine environment, and therefore any calls to App Engine services at the module level will fail on the development server.

    Move the call to memcache inside a function that's called from a request handler, and it should resolve your problem.