Python 全局变量是线程安全的吗?

发布于 2024-08-20 18:46:04 字数 384 浏览 6 评论 0原文

编辑:我问全局变量在像龙卷风这样的单线程Web框架中是否安全,

我使用mongoengine orm,它从全局变量获取数据库连接:

_get_db() # gets the db connection

我还使用龙卷风,一个单线程Python Web框架。在一个特定的视图中,我需要获取一个数据库连接并取消引用一个DBRef对象[类似于外键]:

# dereference a DBRef
_get_db().dereference(some_db_ref)

因为_get_db返回的连接是一个全局变量,是否有可能发生冲突并且错误的值返回到错误的线程?

edit: im asking if global variables are safe in a single-threaded web framework like tornado

im using the mongoengine orm, which gets a database connection from a global variable:

_get_db() # gets the db connection

im also using tornado, a single-threaded python web framework. in one particular view, i need to grab a database connection and dereference a DBRef object [similar to a foreign key]:

# dereference a DBRef
_get_db().dereference(some_db_ref)

since the connection returned by _get_db is a global var, is there a possibility of collision and the wrong value being returned to the wrong thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

○愚か者の日 2024-08-27 18:46:04

与 Python 对象交互时,线程始终需要持有 GIL。保存变量的命名空间是一个 Python 对象(框架对象或字典,取决于它是什么类型的变量。)在多个线程中获取或设置变量始终是安全的。你永远不会得到垃圾数据。

但是,通常的竞争条件确实适用于您获取哪个对象,或者在分配时替换哪个对象。像 x += 1 这样的语句不是线程安全的,因为不同的线程可以在 get 和 store 之间运行,从而更改 x 的值code>,然后您将覆盖它。

Threads are always required to hold the GIL when interacting with Python objects. The namespace holding the variables is a Python object (either a frameobject or a dict, depending on what kind of variable it is.) It's always safe to get or set variables in multiple threads. You will never get garbage data.

However, the usual race conditions do apply as to which object you get, or which object you replace when you assign. A statement like x += 1 is not thread-safe, because a different thread can run between the get and the store, changing the value of x, which you would then overwrite.

瘫痪情歌 2024-08-27 18:46:04

假设 MongoEngine 正在包装 PyMongo(我相信它是),那么你应该没问题。 PyMongo 是完全线程安全的。

Assuming MongoEngine is wrapping PyMongo (and I believe it is), then you should be fine. PyMongo is completely thread-safe.

怼怹恏 2024-08-27 18:46:04

不,但是锁在 python 中使用起来非常简单。使用 try:finally: 模式可确保在修改全局变量后释放锁。

No, but locks are pretty straightforward to use in python. Use the try: finally: pattern to ensure that a lock is released after you modify your global variable.

捎一片雪花 2024-08-27 18:46:04

全局变量并不比其他变量更安全或更不安全。无论操作在不同线程中运行时是否可能失败或返回不正确的结果,最佳实践是您应该保护线程之间共享的数据。

如果我没听错,那么您'询问变量在单线程环境中是否安全。在这种情况下,如果数据在并发进程之间共享,则变量是安全的(毕竟,没有其他正在运行的东西可能会中断它)。

There is nothing about globals that makes them any more or less thread safe than any other variables. Whether or not it's possible for an operation to fail or return incorrect results when run in different threads, the best practice is that you should protect data shared between threads.

If I'm reading you right, you're asking if a variable is safe in a single-threaded environment. In this case, where data is not shared between concurrent processes, the variable is safe (after all, there's nothing else running that may interrupt it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文