簡體   English   中英

通過python或Postgres獨占訪問PostgreSQL表

[英]Exclusive access to a PostgreSQL table through python or Postgres

我有一個表,其中包含存儲在PG中的URL列表,並且我有一個應用程序,該應用程序應該獲取可用URL的子集並對其進行處理。 該應用程序在檢索URL集合時將更新已處理的field = true。 我將在不同的計算機上運行多個實例。 如何確保我對PG的訪問是獨占的,所以我最終不會在不同的計算機上使用相同的URL集?

可以使用LOCK語句在您的Postgresql查詢中完全解決此問題。 這是帶有示例的文檔鏈接:

http://www.postgresql.org/docs/8.1/static/sql-lock.html

您可以在postgresql中以多種方式鎖定行:表級鎖定,行級鎖定和咨詢鎖。

http://www.postgresql.org/docs/9.0/static/explicit-locking.html

但是,就您的情況而言,如果您想要良好的並發性,那么鎖定還遠遠不夠:

update links
set processing = true
where id in (
  select id
  from links
  where not processed
  and not processing
  limit 100
  for update
  )
returning *

額外的processing字段使多個作業可以在不同的行集上工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM