2012-10-03 18 views
1

我正在使用基於RamSession後面的nginx運行我自己的會話運行cherrypy應用程序。問題是會話ID在每個請求上都發生了變化。我相信問題是每次請求發送到不同的工作人員,因此會話被保存,但在下一個可用的工作人員的下一個請求中不能識別(不幸的是,有關事情如何工作的知識有限)。當我將工人數量設置爲1時,一切都按預期工作。我知道我可以使用FileSession或任何類型的基於數據庫的會話處理程序,但只是想知道是否有解決方案。由於使用會話RamSession與nginx後面的cherrypy錯誤

這裏是我的新貴腳本:

description "uwsgi tiny instance" 
start on runlevel [12345] 
stop on runlevel [06] 

exec /home/web/.virtualenvs/myenv/bin/uwsgi --uid web -H /home/web/.virtualenvs/myenv -w myapp.wsgi -p 1 -M -s 127.0.0.1:3031 

這裏是我的會話:

class MySession(sessions.RamSession): 
    def clean_up(self): 
     """Clean up expired sessions.""" 
     now = self.now() 
     for id, (data, expiration_time) in copyitems(self.cache): 
      if expiration_time <= now: 
       try: 
        active = Mongo(ActiveSession).find_one('active', self.cache['active']) 
        Mongo(ActiveSession).remove(active) 
       except: 
        print "Failed to remove active session object." 
       try: 
        del self.cache[id] 
       except KeyError: 
        pass 
       try: 
        del self.locks[id] 
       except KeyError: 
        pass 
     # added to remove obsolete lock objects 
     for id in list(self.locks): 
      if id not in self.cache: 
       self.locks.pop(id, None) 

和我的配置:

config = { 
    '/static': { 
     'tools.staticdir.on': True, 
     'tools.staticdir.dir': os.path.join(current_dir, 'media/public') 
    }, 
    '/fotos': { 
     'tools.staticdir.on': True, 
     'tools.staticdir.dir': os.path.join(current_dir, 'media/fotos') 
    }, 
    '/' : { 
     'tools.sessions.on': True, 
     'tools.sessions.name': 'myapp' 
     'tools.sessions.storage_type': 'my', 
     'engine.autoreload_on': False 
    } 
} 
+0

當你說你的工作人員數量是1時它能夠正常工作,你是在談論uWSGI還是Nginx工作者? – aychedee

+0

我在說uwsgi。在我最初的新貴腳本中,我將-p標誌設置爲3,即會話停止工作。 –

回答

1

你的直覺是正確的:在RamSession有限一次處理1個進程。簡單的解決方案是切換到FileSession(如果您的工作人員都可以訪問相同的文件系統)或數據庫會話。假設你的工作人員分佈很多,最有可能是後者。