我正在尝试使用 psycopg 和多处理插入和更新几百万行。根据http://initd.org/psycopg/docs/usage.html#thread-and-process-safety中的文档,每个孩子都有自己的数据库连接。
但在行刑过程中,只有一个孩子逃跑,其他孩子变成了僵尸。脚本本身非常简单,这里是相同的修剪版本,
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
,但看起来情况并非如此。我错过了一些明显的东西吗?