我正在尝试使用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
表,想确定是否存在升级锁,但似乎并不是这种情况。我是否漏掉了一些明显的东西?