首页 > 解决方案 > cherrypy 未通过 cherrpy.system.exit 退出

问题描述

我正在尝试运行以下简单的 cherrpy 代码gather_keys_oauth2,位于: https ://github.com/orcasgit/python-fitbit/blob/master/gather_keys_oauth2.py

我认为,重要的代码基本上如下:

import cherrypy
import threading

class OAuth2Server:

    def __init__(self):
        self.param = 1

    def browser_authorize(self):
        """
        Open a browser to the authorization url and spool up a CherryPy
        server to accept the response
        """
        cherrypy.quickstart(self)

    @cherrypy.expose
    def index(self):
        """
        Receive a Fitbit response containing a verification code. Use the code
        to fetch the access_token.
        """
        self.param = 2  
        #This doens't work either:
        #threading.Timer(1, cherrypy.engine.exit).start()
        cherrypy.engine.exit()
        return "<h1>wtf</h1>"

s = OAuth2Server()
s.browser_authorize() #hanging here

#Currently this doesn't run, hangs at the previous line
print('hooray! this ran') 

#Note, in reality I want to run more code here

一旦你运行代码,导航到http://127.0.0.1:8080/它应该运行终止代码,但代码永远不会退出,所以我永远不会得到我的“万岁!” 之后留言。为什么我不能阻止cherrypy阻止代码执行?这是在使用 Spyder、Python 3.8 和 cherrypy 18.6.0 的 Mac 上。

编辑:澄清一下,目标是能够在 kill 命令之后继续在主 Python 线程中执行,并且可以在主线程中访问 c​​herrpy 代码中的变量。

这就是我看到的(来自 threading.Timer 调用。它只是挂在最后一条消息上。

[23/Jan/2021:00:13:24] ENGINE Bus STOPPING
[23/Jan/2021:00:13:29] ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(('127.0.0.1', 8080)) shut down
[23/Jan/2021:00:13:29] ENGINE Stopped thread 'Autoreloader'.
[23/Jan/2021:00:13:29] ENGINE Bus STOPPED
[23/Jan/2021:00:13:29] ENGINE Bus EXITING
[23/Jan/2021:00:13:29] ENGINE Bus EXITED
[23/Jan/2021:00:13:29] ENGINE Waiting for child threads to terminate...
[23/Jan/2021:00:13:29] ENGINE Waiting for thread Thread-4.

标签: cherrypy

解决方案


推荐阅读