1

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

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

我知道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({
            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
4

1 回答 1

1

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

于 2013-10-10T22:07:29.380 回答