问题描述
假设我有以下简单的课程:
Let's say I have the following simple class:
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,依此类推.我的问题是:
So when I open my index page the first time I get back 1, the next time 2, then 3, 4, and so on. My questions are:
- 这样做有没有很大的危险,尤其是在线程和多个人同时访问页面的情况下?
- 为了防止出现问题,每次写入成员变量时都必须以某种方式锁定它吗?
- 如果我使用非基本数据类型作为成员(例如我自己的复杂类)而不是像整数这样简单的东西,会发生什么变化吗?
我不完全了解CherryPy的线程如何工作,我想在这个简单的示例中我会担心的是,在一个线程上,test_member可能等于一件事,而从另一个线程访问时,它将完全是一件事情.不同的.如果我丢失了有据可查的东西,我会事先道歉,但是有些谷歌搜索并没有真正找到我想要的东西.我知道对于这样一个简单的示例,有许多相对简单的路径可以在这里解决潜在的问题(保持数据库中变量的状态,或者类似的方式),但是在我的实际用例中不起作用
I don't totally understand how threading with CherryPy works, I suppose my concern in this simple example would be that on one thread the test_member could be equal to one thing, and when accessed from another thread it'd be something totally different. I apologize in advance if I'm missing something that's well documented, but some googling didn't really turn up what I was looking for. I understand for such a simple example there are a number of relatively easy paths that could solve potential problems here (keep the state of the variable in a database, or something along those lines), but that won't work in my actual use case.
推荐答案
存在丢失更新的危险.只需设置值就不需要锁定,因为相对于GIL而言,替换实例变量是原子的(假设它不调用任何特殊方法,等等).但是增加或使用更复杂的变量将需要不同的方案以使其具有线程安全性.
There's a danger there of lost updates. Just setting the value shouldn't need to lock, since replacing an instance variable is atomic with respect to the GIL (assuming it doesn't call any special methods, etc). But incrementing or using more complex variables will need different schemes to make them threadsafe.
CherryPy中的共享访问通常与任何其他Python程序没有什么不同.与其在此处冗长地讨论所有这些选项,不如将您引导到 http://effbot.org/zone/thread-synchronization.htm 如前所述,相对于GIL来说,替换实例变量可能是原子的,因此是线程安全的,但增量不是.
Shared access in CherryPy is generally no different than any other Python program. Rather than a long rehash of all those options here, it's best to direct you to http://effbot.org/zone/thread-synchronization.htm As it mentions, replacing an instance variable is probably atomic with respect to the GIL and thereby thread-safe, but incrementing is not.
CherryPy仅添加了相反方向的一些帮助器:当您不想想要共享时: cherrypy.request
和 cherrypy.response
为每个请求/响应新创建(并适当销毁)对象-如果只想在请求期间将数据保留在 cherrypy.request.foo
中,则可以随意将数据保留在 cherrypy.request.foo
中.
CherryPy only adds some helpers in the opposite direction: when you don't want to share: the cherrypy.request
and cherrypy.response
objects are newly created (and properly destroyed) for each request/response--feel free to stick data in cherrypy.request.foo
if you want to keep it around for the duration of the request only.
这篇关于CherryPy,线程和成员变量;潜在的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!