python - 如何使用 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() 的工作原理?
解决方案
追加比初始插入花费的时间要长得多。您可能想尝试使用 -C 和合理数量的缓存(默认为 800MB)。此外,我们谈论的是初始插入后的几个小时。因此,也许您想确保始终先插入最大的文件。如果您的文件非常大,也可以使用 --slim 以确保在缓存耗尽时不会崩溃。
推荐阅读
- apache-spark - 是否可以按几列分组并显示数据集的所有列?
- android - 新项目(聊天应用)Android Studio 上的回收站视图动画
- database - 如何将 API 密钥集成到 IBM Watson Assistant 上的 webhook 中?
- paho - 无法使用 paho 客户端连接到 IoT
- python - 使用 pandas 复制非 na 行以填充非 na 列
- python-3.x - 如何从两个不均匀列表中获取字典,对相同键的所有值进行分组而不删除任何值?
- apache-spark - spark3 因 py4j.protocol.Py4JJavaError 而崩溃
- python - Python套接字recv()由于硬件而挂起?
- javascript - 如何将 d3.js 与 javascript 文件一起使用,而不是在