如何通过django + uwsgi创建缓存?

时间:2016-03-16 12:42:11

标签: django caching uwsgi

代码适用于本地,但无法在服务器中运行! 我有一个vps(ubuntu):我使用的是uWSGI。

我有一个管理在线用户的课程。

from django.core.cache import cache

class OnlineUser(object):
    def __init__(self, key):
    self.key = key
    self.human_env = Environment(PROBABILITY_ENV)
    self.robot_env = Environment(PROBABILITY_ENV)
    human = Human()
    robot = Agent(configure[COST_MOVEMENT][0], configure[COST_MOVEMENT][1], configure[COST_MOVEMENT][2])
    self.robot_env.agent = robot
    self.human_env.agent = human

    f_learning = 'agent_p%.2f_d%.2f_l%.2f_e%.2f_c%.2d.ai' % (
        PROBABILITY_ENV, configure[COST_MOVEMENT][0], configure[COST_MOVEMENT][1], configure[COST_MOVEMENT][2],
        COST_MOVEMENT)
    f_learning = os.path.join(PROJECT_ROOT, 'chahchi', f_learning)

    try:
        with open(f_learning, 'rb') as agent_:
            robot.policy = pickle_load(agent_)
    except:
        self.robot_env.learn()
        with open(f_learning, 'wb') as output:
            pickle_dump(robot.policy, output)

    self.update_cache()

    def next_round(self):
       self.human_env.next_round()
       self.robot_env.next_round()

    def update_cache(self):
       cache.set(self.key, self)

    def __eq__(self, other):
       return self.key == other.key

def chahchi(request):
   request.session['chahchi'] = True
   user = OnlineUser(request.session.session_key)

   ground = [[0 for j in xrange(8)] for i in xrange(8)]
   for i, j, value in State.points:
      ground[i][j] = value
   for i, j, in Environment.wells:
      ground[i][j] = -200

   robot = [0, 7]
   return render_to_response('chahchi/chahchi.html', {
    'ground': ground,
    'robot': robot
}, RequestContext(request))

@csrf_exempt
def chahchi_ajax(request):
   user = cache.get(request.session.session_key)
   if not request.session['chahchi']:
      return HttpResponse(status=404)

   human_action = request.POST['action']
   robot_env, human_env = user.robot_env, user.human_env

  if not robot_env.test_info['finish']:
     r_action_name, r_test_info = robot_env.next_step()
  else:
     r_action_name = None
     r_test_info = robot_env.test_info
  if not human_env.test_info['finish']:
     h_action_name, h_test_info = human_env.next_step(action=human_action)
  else:
     h_action_name = None
     h_test_info = human_env.test_info

  if robot_env.test_info['finish'] and human_env.test_info['finish']:
     user.next_round()
     user.update_cache()
  else:
     user.robot_env = robot_env
     user.human_env = human_env
     user.update_cache()

  response_data = {
    'robot': {
        'test_info': r_test_info,
        'action': r_action_name,
    },
    'human': {
        'test_info': h_test_info,
        'action': h_action_name,
    },
    'game': {
        'counter_episode': 0,
        'finish': False
    }
}
   return HttpResponse(json_dumps(response_data), content_type='application/json')

settings.py

MIDDLEWARE_CLASSES = (
"mezzanine.core.middleware.UpdateCacheMiddleware",

'django.contrib.sessions.middleware.SessionMiddleware',
# Uncomment if using internationalisation or localisation
# 'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',

"mezzanine.core.request.CurrentRequestMiddleware",
"mezzanine.core.middleware.RedirectFallbackMiddleware",
"mezzanine.core.middleware.TemplateForDeviceMiddleware",
"mezzanine.core.middleware.TemplateForHostMiddleware",
"mezzanine.core.middleware.AdminLoginInterfaceSelectorMiddleware",
"mezzanine.core.middleware.SitePermissionMiddleware",
# Uncomment the following if using any of the SSL settings:
# "mezzanine.core.middleware.SSLRedirectMiddleware",
"mezzanine.pages.middleware.PageMiddleware",
"mezzanine.core.middleware.FetchFromCacheMiddleware",)

local_settings.py

我测试了这个

CACHES = {
    'default': {
       'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
       'LOCATION': '/var/tmp/django_cache',
    }
}

CACHES = {
    'default': {
       'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
       'LOCATION': '127.0.0.1:11211',
    }
}

但在服务器中无法正常工作。

在调用my_view_ajax后首先调用函数my_view。

当调用my_view_ajax时,我收到错误500,

错误:

'NoneType' object has no attribute 'robot_env'

0 个答案:

没有答案
相关问题