多处理psycopg2 僵尸儿童
我正在尝试使用 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 来检查表是否具有升级锁,但看起来情况并非如此。我错过了一些明显的东西吗?
I am trying to insert and update a few million rows using psycopg and multiprocessing. Going by the documentation found in http://initd.org/psycopg/docs/usage.html#thread-and-process-safety, each child has its own connection to the DB.
But during the course of execution, only one child runs while the others become zombies. The script in itself is pretty simple and here is a trimmed version of the same,
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()
I also checked if the tables have an escalated lock by peeking into pg_locks
, but it looks like its not the case. Am I missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的进程变成僵尸,因为作业已完成但进程尚未加入。
我通过这个测试重现了您的问题(我添加了睡眠来模拟长时间作业):
执行此操作时,在 3 个进程打印出它们将停止后,它们会出现在 ps 视图中(它们不再移动,但实际上不再移动)死了,因为父亲还抱着它们)。
如果我用这个替换主要部分,我就不再有僵尸了:
your processes become zombies because there jobs are finished but the processes are not joined.
I reproduced your problem with this single test (I added sleep to simulate long jobs) :
when executing this, after the 3 processes print that they will stop, they become in the ps view (they don't move anymore, but are not really dead because the father still hold them).
If I replace the main part with this, i have no more zombies :