gpt4 book ai didi

python - 如何保持愉快的 session 彼此不同

转载 作者:太空宇宙 更新时间:2023-11-03 18:47:26 24 4
gpt4 key购买 nike

我正在努力让愉快的 session 来做我想做的事情。我有一个与用户进行单独聊天的应用程序(每个对话都有一个唯一的 dlg_nbr)。它似乎工作正常(参见下面日志输出中的前两个周期),直到单独的对话混合在一​​起(当我同时发布时),并且在 user2 的响应之前,user1 开始获得针对 user2 的响应(日志输出中的第三个周期)已处理。

我本以为,如果我的主函数 (think() ) 使用并返回一个 dlg_nbr,对于每个用户来说都是唯一的,我将其存储为 session [dlg_nbr],这将确保跨响应/请求周期的连续性,该 session 是每个用户都是唯一的,对吗?

我知道cherrypy的多线程可能意味着事情是并行处理的,但是 session 肯定不应该混合吗?这让我抓狂,这是 Cherrypy 的一个功能吗?我做错了什么?

cherrypy代码

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 帖子:

$.ajax({
url: '/rsp',
data: {stm: input_string},
type: "POST",
timeout: 10000,
success: function(response) {......}

日志输出:

# 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

最佳答案

我现在解决了这个问题。为了方便起见,我将值分配给临时全局变量。看来这些是在cherrypy的多个线程之间共享的。解决方案是不使用全局变量,而是在局部范围内显式声明变量。现在工作正常

关于python - 如何保持愉快的 session 彼此不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19192182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com