【发布时间】:2014-11-29 15:12:23
【问题描述】:
我是网络开发的新手。第一次使用cherrypy。
import cherrypy
import src.main
class Interface:
def __init__(self):
self.mypf = src.main.pf()
commonhtmlString = """<form method="get" action="generate">
<input type="text" value="" name="l" />
<input type="text" value="" name="r" />
<button type="submit">Give it now!</button>
</form>"""
@cherrypy.expose
def index(self):
return Interface.commonhtmlString
@cherrypy.expose
def generate(self, l, r):
self.mypf = src.main.pf()
self.mypf.getLocalityInfo(l, r)
return Interface.commonhtmlString + self.mypf.printC()
@cherrypy.expose
def explore(self, c):
return Interface.commonhtmlString + self.mypf.printIC(c)
cherrypy.quickstart(Interface())
以上代码运行良好。但是当我打开新浏览器或新标签并更改参数并生成新输出时......它反映在所有打开的链接中并导致错误。
考试。假设我输入 l=x r=xx,得到的结果是 xxx。现在,如果我在另一个选项卡中打开 url 并输入 l=y r=yy,我得到的结果是 yyy。现在发生的事情是上一个选项卡没有持续到 xxx,因此如果我单击 xxx 中的一些链接,它们要么转到 yyy 等效链接,要么导致错误。
有人可以帮忙解决这个问题吗? 您可以解释或提供任何外部链接或概念。准备阅读。 提前致谢。
【问题讨论】:
-
不清楚你指的是什么链接,
pf做了什么。很可能您没有意识到Interface实例只创建一次,它会在请求之间保留数据(并且您需要确保全局数据的线程安全)。如果没有帮助,请使用可运行的代码编辑您的问题。
标签: python thread-safety cherrypy server