首页 > 解决方案 > 如何使用 pexpect 在多个文件上运行 osm2pgsql?坚持“使用 PBF 解析器”。

问题描述

我正在尝试从多个 .pbf 文件创建单个 SQL 表。

我正在使用 osm2pgsql 将文件加载到远程数据库中,并尝试使用 python 和 pexpect 自动化该过程。

虽然第一个 osm2pgsql 命令成功运行,但后续命令在打印“使用 PBF 解析器”后似乎卡住了。

这是我的代码:

child = pexpect.spawn('bash', timeout=20000)
child.logfile_read = sys.stdout.buffer # show output for debugging

filenames = os.listdir('pbf_files')
for i, filename in enumerate(filenames):

    print(filename)
    upload_command_args = [
        "pbf_files/{}".format(filename),
        "-l",
        "-s",
        "-d", db_name,
        "-U", username,
        "-P", port,
        "-H", host,
        "-W",
        "-S", "default.style",
        "-r", "pbf",
        "-p", table_name,
        "--hstore",
        ]

    # Need the append option since table already exists after first iteration
    if i > 0:
        upload_command_args = upload_command_args + ["--append"]

    print(upload_command_args)
    child.sendline('osm2pgsql ' + ' '.join(upload_command_args))
    child.expect('Password:')
    child.sendline('myFakePass')
    child.expect('Osm2pgsql took .+ overall')

child.close()
sys.exit(child.status)

第 0 次迭代正常运行,但第 1 次在 shell 打印后卡住:

Reading in file: pbf_files/my_partition_1.pbf
Using PBF parser.

我是否误解了 .expect() 的工作原理?

标签: pythonpexpectosm2pgsqlosm.pbf

解决方案


追加比初始插入花费的时间要长得多。您可能想尝试使用 -C 和合理数量的缓存(默认为 800MB)。此外,我们谈论的是初始插入后的几个小时。因此,也许您想确保始终先插入最大的文件。如果您的文件非常大,也可以使用 --slim 以确保在缓存耗尽时不会崩溃。


推荐阅读