【问题标题】:When one GET request starts working, I want set a variable which can immediately be read by other GET requests当一个 GET 请求开始工作时,我想设置一个可以立即被其他 GET 请求读取的变量
【发布时间】:2020-06-11 15:58:07
【问题描述】:

在请求完成之前,似乎模块级变量实际上并未“更改”。 我需要它立即更新,以便其他请求可以知道该工作正在处理中。

这是一个问题的例子:

import cherrypy
import time


@cherrypy.expose
class CherryPySleeps(object):
    status = "not sleeping"

    @cherrypy.tools.accept(media='text/plain')
    def GET(self):
        if CherryPySleeps.status == "not sleeping":
            CherryPySleeps.status = "sleeping"
            time.sleep(10)
            CherryPySleeps.status = "not sleeping"
            return "I finished sleeping"
        else:
            return "I am sleeping, shhh"


if __name__ == '__main__':
    conf = {
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.sessions.on': True,
            'tools.response_headers.on': True,
            'tools.response_headers.headers': [('Content-Type', 'text/plain')],
        }
    }
    cherrypy.quickstart(CherryPySleeps(), '/', conf)

当我打开 2 个浏览器选项卡时,在第一个页面请求页面,然后等待一秒钟,然后在第二个页面请求页面,我在每个选项卡上暂停 10 秒,然后“我已完成睡眠”。

我想要的是第二个标签快速响应,“我在睡觉,嘘”

【问题讨论】:

    标签: get cherrypy


    【解决方案1】:

    如果您想保证请求(线程)之间的某种同步性,您可以对读写都使用锁(或在if 条件下使用锁)。顺便说一句,我无法复制您描述的确切条件(它对我有用),但至少锁应该保证它将是一个一致的结果。

    看看这个例子:

    import time
    import threading
    
    import cherrypy
    
    
    @cherrypy.expose
    class CherryPySleeps(object):
        def __init__(self):
            # share the same lock among read and write operations,
            # the read is not "strictly" required, but you can use
            # it to make sure there is no write happening at the
            # same time
            self._status_lock = threading.Lock()
            # the internal status variable
            self._status = "not sleeping"
    
        @property
        def status(self):
            with self._status_lock:
                return self._status
    
        @status.setter
        def status(self, new_status):
            with self._status_lock:
                self._status = new_status
    
        @cherrypy.tools.accept(media='text/plain')
        def GET(self):
            if self.status == "not sleeping":
                self.status = "sleeping"
                time.sleep(10)
                self.status = "not sleeping"
                return "I finished sleeping"
            else:
                return "I am sleeping, shhh"
    
    
    if __name__ == '__main__':
        conf = {
            '/': {
                'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
                'tools.sessions.on': True,
                'tools.response_headers.on': True,
                'tools.response_headers.headers': [('Content-Type', 'text/plain')],
            }
        }
        cherrypy.quickstart(CherryPySleeps(), '/', conf)
    

    还有其他方法可以做到这一点,使用 CherryPy 中的插件系统,但我认为对于这个例子来说没有必要。

    【讨论】:

    • 原来我在我的浏览器中测试,它是“等待缓存”,这导致它在处理第二个之前等待一个 url 加载完成。您的回答很完整,我从中找到了价值,因此我将其标记为已回答。
    猜你喜欢
    • 2013-11-02
    • 2012-02-04
    • 2021-05-11
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多