简体   繁体   English

如何在另一个进程的原子事务期间读取修改后的表?

[英]How to read a modified table during an atomic transaction from another process?

With Django I am doing a big processing with a background process that does a lot of updates in the database.使用 Django,我正在使用后台进程进行大量处理,该进程在数据库中进行大量更新。 I protect the whole big processing with an atomic transaction, thus if it fails, the database go back to its previous state. In order to follow in which step the processing is, I update a table dedicated for that.我用原子事务保护整个大处理,因此,如果它失败,数据库 go 会恢复到它以前的 state。为了跟踪处理的步骤,我更新了一个专用于该处理的表。 I would like to display in real time in which step the processing is on a web site (so, from another process).我想实时显示处理在 web 站点上的哪个步骤(因此,来自另一个进程)。 Unfortunatly, I cannot see steps changing in the dedicated table because the transaction is committed only at the end of the processing.不幸的是,我看不到专用表中的步骤发生变化,因为事务仅在处理结束时提交。 To summary, I have someting like this:总而言之,我有这样的事情:

For the background process:对于后台进程:

with transaction.atomic():
    task.step = 'Step 1'
    task.save()

    ... do step 1 processing ...

    task.step = 'Step 2'
    task.save()

    ... do step 2 processing ...

    task.step = 'Finised'
    task.save()

On web site, I will have an Ajax view that will do something like that:在 web 站点上,我将有一个 Ajax 视图,它将执行类似的操作:

GetProcessingProgressView(View):
    def get(self, request, **kwargs):
        ... 
        task = Task.objects.get(....)
        data = { 'step' : task.step }
        return HttpResponse(json.dumps(data), content_type='application/json')

The only step displayed on the web site is 'Finished' (that is when the transaction is committed) web 站点上显示的唯一步骤是“已完成”(即提交交易时)

How can I follow my big processing steps from another process while the transaction is not committed yet?在交易尚未提交的情况下,如何从另一个流程中遵循我的大处理步骤? Is there a way to exclude a table from the atomic transaction?有没有办法从原子事务中排除一个表? Actually, the only solution I see is to use another database for that, or use files instead of database: what would you do instead?实际上,我看到的唯一解决方案是为此使用另一个数据库,或者使用文件而不是数据库:你会怎么做?

You would have to choose a communication mechanism that is outside the database, since almost everything in PostgreSQL is transactional.您将不得不选择数据库外部的通信机制,因为 PostgreSQL 中的几乎所有内容都是事务性的。

As a workaround, you could use the dblink extension to open a second database session to the same database using a database function and update the progress tracking table this way.作为解决方法,您可以使用dblink扩展打开第二个数据库 session 到使用数据库 function 的同一数据库,并以此方式更新进度跟踪表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM