Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多处理+;psycopg2僵尸儿童_Python_Multiprocessing_Psycopg2_Zombie Process - Fatal编程技术网

Python 多处理+;psycopg2僵尸儿童

Python 多处理+;psycopg2僵尸儿童,python,multiprocessing,psycopg2,zombie-process,Python,Multiprocessing,Psycopg2,Zombie Process,我正在尝试使用psycopg和多处理来插入和更新几百万行。根据中的文档,每个孩子都有自己与数据库的连接 但在执行过程中,只有一个孩子在奔跑,而其他孩子则变成了僵尸。脚本本身非常简单,这里是相同脚本的精简版本 import os import psycopg2 from multiprocessing import Process def _target(args): # Each forked process will have its own connection # h

我正在尝试使用psycopg和多处理来插入和更新几百万行。根据中的文档,每个孩子都有自己与数据库的连接

但在执行过程中,只有一个孩子在奔跑,而其他孩子则变成了僵尸。脚本本身非常简单,这里是相同脚本的精简版本

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
检查了表是否有升级锁,但看起来情况并非如此。我是否遗漏了一些明显的东西?

您的流程会变成僵尸,因为有作业已完成,但流程未连接。 我用这个测试重现了你的问题(我添加了睡眠来模拟长时间的工作):

执行此操作时,在3个进程打印出它们将停止后,它们将进入ps视图(它们不再移动,但并不是真的死了,因为父亲仍然持有它们)

如果我用这个替换主要部分,我就没有更多的僵尸了:

if __name__ == '__main__':
    args = "Foo"
    processes = []
    for i in xrange(3):
        p = Process(target=_target, args=(args,))
        processes.append(p)
        p.start()
    for p in processes:
        p.join()
    import time
    time.sleep(10)

get\u db\u连接的作用是什么?是创建新连接还是返回共享连接?根据你网站上的文档,它应该正在创建一个新连接。Philip,不,它不使用共享连接。为每个分叉子级创建一组新的连接和游标。(应该是create_db_connection())塞德里克,我遇到的问题是只有一个孩子会运行,而其他孩子会变成僵尸。顺便说一句,这个问题原来是Postgres的僵局
if __name__ == '__main__':
    args = "Foo"
    processes = []
    for i in xrange(3):
        p = Process(target=_target, args=(args,))
        processes.append(p)
        p.start()
    for p in processes:
        p.join()
    import time
    time.sleep(10)