psycopg2错误:DatabaseError:错误,没有来自libpq的消息

use*_*599 12 python postgresql psycopg celery python-multiprocessing

我有一个应用程序,它将csv文件中的数据解析并加载到Postgres 9.3数据库中.在串行执行中,插入语句/游标执行没有问题.

我在混合中添加了celery以添加并行解析和插入数据文件.解析工作正常.但是,我去运行插入语句,我得到:

[2015-05-13 11:30:16,464:  ERROR/Worker-1] ingest_task.work_it: Exception
    Traceback (most recent call last):
    File "ingest_tasks.py", line 86, in work_it
        rowcount = ingest_data.load_data(con=con, statements=statements)
    File "ingest_data.py", line 134, in load_data
        ingest_curs.execute(statement)
    DatabaseError: error with no message from the libpq
Run Code Online (Sandbox Code Playgroud)

小智 10

我在多处理时遇到了类似的问题engine.execute().我最后通过engine.dispose()在子进程应该输入的函数的第一行中添加正确来解决这个问题,如官方文档中所建议的:

当程序使用多处理或者fork()Engine对象复制到子进程时,Engine.dispose()应该调用它以便引擎在该fork的本地创建全新的数据库连接.数据库连接通常跨越进程边界.

  • 确切地。任何多处理/工作线程和数据库连接设置都会发生这种情况。您通常需要在每个进程的基础上创建一个新的数据库连接。 (2认同)