带importlib的Python多处理

2024-04-26 05:24:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用多重处理来导入模块。在'conf'变量中,我有一个很大的模块名列表。函数从模块中加载类名称,并检查是否为这些类创建了表,如果没有-它们将在数据库中创建。你知道吗

import importlib
from mymodule import exceptions
from datetime import datetime
from multiprocessing import Process

def process_import(ormModule):
    print "importing ...", ormModule
    t1 = datetime.now()
    try:
        importlib.import_module(ormModule)
    except ImportError:
        exceptions.log("Unable to import ORM module %s", ormModule)
        failed.append(ormModule)
    except:
        exceptions.log("Exception while importing ORM module %s", ormModule)
        failed.append(ormModule)
    t2 = datetime.now()
    total = t2 - t1
    print "finished importing %s %s" % (ormModule, total.total_seconds())

def loadClasses(conf):

    for ormModule in conf:
        Process(target=process_import, args=(ormModule,)).start()

有些模块可以导入,但有些没有

sqlalchemy.exc.OperationalError: (OperationalError) server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.
 'select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where n.nspname=current_sch() and relname=%(name)s' {'name': u'document1'}

我怎样才能解决这个问题?我在用PostgreSQL。你知道吗


Tags: 模块thefromimportdatetimeconfdefimportlib