【发布时间】:2011-04-26 17:49:24
【问题描述】:
我正在尝试使用 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 来检查表是否具有升级锁,但看起来情况并非如此。我错过了什么明显的东西吗?
【问题讨论】:
-
get_db_connection 是做什么的?是创建新连接还是返回共享连接?根据您找到的文档,它应该正在创建一个新连接。
-
Philip,不,它不使用共享连接。为每个分叉的孩子创建一组新的连接和游标。 (应该是 create_db_connection() )
标签: python multiprocessing psycopg2 zombie-process