cherrypy - 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 线程中执行,并且可以在主线程中访问 cherrpy 代码中的变量。
这就是我看到的(来自 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.
解决方案
推荐阅读
- reactjs - 如何修改firebase数据库以便我可以设置已读或未读状态
- c++ - 抛出 C++ 异常:读取访问冲突
- spring - 通过百里香形式添加文本格式
- c# - 带有 Microsoft.Extensions.DependencyInjection 和 asp.net web api 2 的拦截器,用于横切关注点,例如日志记录
- rust - 在`main`中指定值的静态生命周期,以便回调可以借用
- python - BeautifulSoup - 获取属性的所有值
- servlets - GWT 文件上传表单提交 POST 请求无法读取文件
- maven - 调试“jOOQ代码生成工具配置错误”
- c# - 从 IEnumerable 列表中过滤字符串 C# linq
- swift - 如何调整图像视图的大小?