我想插入並更新使用psycopg和multiprocessing幾百萬行。通過http://initd.org/psycopg/docs/usage.html#thread-and-process-safety中發現的文檔,每個孩子都有自己的連接到數據庫。multiprocessing + psycopg2殭屍兒童
但是在執行過程中,只有一個孩子在其他人變成殭屍時運行。在本身的腳本是非常簡單,這裏是相同的修剪版本,
import os
import psycopg2
from multiprocessing import Process
def _target(args):
# Each forked process will have its own connection
# http://initd.org/psycopg/docs/usage.html#thread-and-process-safety
conn = get_db_connection()
# Stuff seems to execute till this point in all the children
print os.getpid(), os.getppid()
# Do some updates here. After this only one child is active and running
# Others become Zombies after a while.
if __name__ == '__main__':
args = "Foo"
for i in xrange(3):
p = Process(target=_target, args=(args,))
p.start()
我還檢查,如果表按偷看pg_locks
有逐步升級的鎖,但它看起來像它的情況並非如此。我錯過了明顯的東西嗎?
是什麼get_db_connection辦?它是創建一個新的連接還是返回一個共享連接?根據你選擇的文檔,它應該創建一個新的連接。 – 2011-04-26 20:02:42
Philip,不,它不使用共享連接。爲每個分叉的子項創建一組新的連接和光標。 (應該是create_db_connection()) – sudharsh 2011-04-27 05:00:57