【发布时间】:2019-06-27 18:49:59
【问题描述】:
我有一个脚本,它接受input a list of filenames 并循环它们以为每个输入文件生成一个输出文件,所以我认为这种情况可以很容易地并行化。
我有一台 8 核机器。
我尝试在此命令上使用-parallel 标志:
python perfile_code.py list_of_files.txt
但我无法使其工作,即具体问题是:如何在 bash 中使用并行与 Linux 中的 python 命令,以及上述特定情况的参数。
有一个 Linux 并行命令 (sudo apt-get install parallel),我在某处读到它可以完成这项工作,但我不知道如何使用它。
大部分互联网资源都在 python 中解释了如何做到这一点,但它可以在 bash 中完成吗?
请帮忙,谢谢。
Based on an answer, here is a working example that is still not working, please suggest how to make it work.
我有一个包含 2 个文件的文件夹,我只想在此示例中并行创建具有不同名称的副本。
# filelist is the directory containing two file names, a.txt and b.txt.
# a.txt is the first file, b.xt is the second file
# i pass an .txt file with both the names to the main program
from concurrent.futures import ProcessPoolExecutor, as_completed
from pathlib import Path
import sys
def translate(filename):
print(filename)
f = open(filename, "r")
g = open(filename + ".x", , "w")
for line in f:
g.write(line)
def main(path_to_file_with_list):
futures = []
with ProcessPoolExecutor(max_workers=8) as executor:
for filename in Path(path_to_file_with_list).open():
executor.submit(translate, "filelist/" + filename)
for future in as_completed(futures):
future.result()
if __name__ == "__main__":
main(sys.argv[1])
【问题讨论】:
-
为什么要投票赞成关闭?这是一个非常具体的问题,询问如何在 bash 和 python 中使用并行以及参数。我已编辑问题以使其更清楚,请重新考虑。
-
您对并行性主题缺乏基本的了解。
-parallel不是 Python 的有效命令行选项。并行操作的编程通常需要程序员主动开发策略。我建议在谷歌上搜索“python 并行”。 -
@Ouroborus 不,不考虑这个opensource.com/article/18/5/gnu-parallel 我想与这个并行运行一个python程序..对于一个非常特殊的情况..如果一个任意转换程序可以通过管道传输到并行..为什么不使用 python 程序?
-
这仍然需要您了解并行性的一般工作原理以及您的软件能够在该环境中运行。正如您所描述的,您当前的 python 脚本不会从 gnu
parallel中受益。阅读和理解您链接的文章将大大有助于您了解您需要做什么。 -
没有交钥匙
--parallel标志。您需要自己编写并行性,请参阅:multiprocessing
标签: python linux python-3.x python-2.7 parallel-processing