有没有办法在基于 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.
发布评论
评论(1)
我想说那是不可能的。 据我所知,每个线程都有自己的 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
.