在 python/django 中的线程之间共享事务范围? (PostgreSQL)

发布于 2024-07-23 21:01:25 字数 332 浏览 3 评论 0 原文

有没有办法在基于 django 的代码中的两个线程之间共享相同的事务?

问题是我有 1.1 的 TestCase(那些将单个测试包装到事务中的测试用例),旨在测试在不同线程中运行的代码[一种异步测试]。 因此,这些测试创建了一些旨在供第二个线程使用的数据。 显然,由于该数据是在事务范围内创建的,因此它对第二个线程不可见。 但由于这基本上应该与 PgSQL 具有相同的连接(应该吗?)我希望有一种方法可以共享此事务范围,以便我的第二个线程可以访问其中添加的数据?..

有什么想法吗?

数据库是PgSQL 8.3,驱动程序是postgresql_psycopg2。 姜戈——树干。

Is there any way to share same transaction between two threads in a django-based code?

The problem is that I have 1.1's TestCase (those that wrap individual tests into transactions) that are intended to test code that is running in a different thread [a sort of asynchronous testing]. So these test create some data that is intended to be used by this second thread. Obviously, since this data is created within a transaction scope, it is not visible to the second thread. But since that should basically be the same connection to PgSQL (should it?) I hope there is a way to share this transaction scope so my second thread can access data being added within it?..

Any idea?

Database is PgSQL 8.3, driver is postgresql_psycopg2. Django — trunk.

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

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

发布评论

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

评论(1

谜兔 2024-07-30 21:01:25

我想说那是不可能的。 据我所知,每个线程都有自己的 PostgreSQL 会话,以便能够并发运行。 鉴于 PostgreSQL 是一个 MVCC 数据库,一个线程将无法访问另一个线程的更改,直到事务已提交 – 在 Django 1.1 TestCase 的情况下不会提交事务。

如果您需要测试同时运行的内容,我很确定您需要使用 TransactionTestCase

I'd say that's impossible. To my knowledge, each thread has its own PostgreSQL session to be able to run concurrently. And given that PostgreSQL is an MVCC database, one thread will not have access to the other's changes, until the transaction is committed – which it won't be in the case of a Django 1.1 TestCase.

If you need to test stuff that runs concurrently, I'm pretty sure that you need to use a TransactionTestCase.

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