Reputation: 15
I'm struggling to get cherrypy sessions to do what I would like. I have an app that engages in individual chats with the user (each dialogue has a unique dlg_nbr). It seems to work OK (see first two cycles in log output below) until the separate dialogues get mixed (when I post simultaneously) , and user1 starts to get the responses aimed at user2 (third cycle in log output), before user2's responses are processed.
I would have thought that if my main function (think() ) uses and returns a dlg_nbr, unique for each user, that I store as session[dlg_nbr], this would ensure continuity across response/request cycles, The session is unique for each user, right?
I understand that cherrypy's multi threading may mean things are process in paralell, but surely the sessions should not get mixed? This is driving me nuts, is this a feature in cherrypy, what am I doing wrong?
cherrypy code
class Web:
def rsp(self, stm=''):
print ' 10 SESSION ID ON REQUEST', cherrypy.session.id
dlg_nbr = cherrypy.session.get('dlg_nbr')
# main logic of app
rsp, dlg_nbr = B.think(stm, dlg_nbr)
print ' 20 SESSION ID AFTER PROCESSING REQUEST', cherrypy.session.id
cherrypy.session['dlg_nbr'] = dlg_nbr
return rsp
if __name__ == '__main__':
cherrypy.config.update(
{'server.socket_host': config.HOST,
'server.socket_port': config.PORT})
STATIC_DIR = os.path.join(os.path.abspath("."), u"static")
dev_config = {
'/': {'tools.caching.on': False, 'tools.sessions.on': True},
'/static': {'tools.staticdir.on': True,
'tools.staticdir.dir': STATIC_DIR}}
cherrypy.quickstart(Web(), config=dev_config)
ajax post:
$.ajax({
url: '/rsp',
data: {stm: input_string},
type: "POST",
timeout: 10000,
success: function(response) {......}
log output:
# Expected result for remote user
192.168.1.2 - - [04/Oct/2013:23:22:05] "POST /rsp HTTP/1.1" 200 65 "http://192.168.1.4:8003/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"
10 SESSION ID ON REQUEST 6125bf1b113e2da09e4653e8eccf0bd3b7240ddb
20 SESSION ID AFTER PROCESSING REQUEST 6125bf1b113e2da09e4653e8eccf0bd3b7240ddb
# Expected result for localhost
127.0.0.1 - - [04/Oct/2013:23:22:09] "POST /rsp HTTP/1.1" 200 8 "http://localhost:8003/" "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36"
10 SESSION ID ON REQUEST d09998f541d6a19742b8d73314a1dba6bba42a84
20 SESSION ID AFTER PROCESSING REQUEST d09998f541d6a19742b8d73314a1dba6bba42a84
# Local host seems to get result of post made by remote user (when submitting stm at the same time)
192.168.1.2 - - [04/Oct/2013:23:22:14] "POST /rsp HTTP/1.1" 200 33 "http://192.168.1.4:8003/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"
10 SESSION ID ON REQUEST 6125bf1b113e2da09e4653e8eccf0bd3b7240ddb
10 SESSION ID ON REQUEST d09998f541d6a19742b8d73314a1dba6bba42a84
20 SESSION ID AFTER PROCESSING REQUEST 6125bf1b113e2da09e4653e8eccf0bd3b7240ddb
# finally remote user gets expected result, (but seems out of synch)
127.0.0.1 - - [04/Oct/2013:23:22:19] "POST /rsp HTTP/1.1" 200 17 "http://localhost:8003/" "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36"
20 SESSION ID AFTER PROCESSING REQUEST d09998f541d6a19742b8d73314a1dba6bba42a84
Upvotes: 1
Views: 1199
Reputation: 15
I fixed this now. I was assigning values to a temporary global variable for convenience. It seems that these are shared across cherrypy's multiple threads. The solution was to not use globals but explicitly declare variables in local scope. Now works fine
Upvotes: 1