假设我有以下简单的类:
import cherrypy
import os
class test:
test_member = 0;
def __init__(self):
return
def index(self):
self.test_member = self.test_member + 1
return str(self.test_member)
index.exposed = True
conf = os.path.join(os.path.dirname(__file__), 'config.ini')
if __name__ == '__main__':
# CherryPy always starts with app.root when trying to map request URIs
# to objects, so we need to mount a request handler root. A request
# to '/' will be mapped to HelloWorld().index().
cherrypy.config.update({'server.socket_host': '0.0.0.0'})
cherrypy.quickstart(test(), config=conf)
else:
# This branch is for the test suite; you can ignore it.
cherrypy.config.update({'server.socket_host': '0.0.0.0'})
cherrypy.tree.mount(test(), config=conf)
因此,当我第一次打开索引页面时,我会返回 1,下一次返回 2,然后是 3、4,依此类推。我的问题是:
- 这有什么大的危险,特别是线程和多人同时访问页面?
- 为了防止出现问题,我是否必须在每次写入成员变量时以某种方式锁定它?
- 如果我使用非基本数据类型作为成员(例如我自己的复杂类)而不是像整数这样简单的东西,有什么改变吗?
我不完全理解 CherryPy 的线程是如何工作的,我想我在这个简单的例子中担心的是,在一个线程上 test_member 可能等于一件事,而当从另一个线程访问时,它会完全不同。如果我遗漏了一些有据可查的东西,我提前道歉,但是一些谷歌搜索并没有真正找到我想要的东西。我知道对于这样一个简单的示例,有许多相对简单的路径可以解决这里的潜在问题(将变量的状态保留在数据库中,或者类似的东西),但这在我的实际用例中不起作用.