Python 中的并行程序没有输出
Parallel Program in Python produces no output
我有一个简单的任务。对于大量文件,特定功能需要 运行。这个任务可以很容易地并行化。
这是工作代码:
# filelist is the directory containing two file, a.txt and b.txt.
# a.txt is the first file, b.xt is the second file
# I pass a file that lits the names of the two files 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])
但是,没有创建新文件,即文件夹不包含 a.txt.x 和 b.txt.x 文件。
上面的代码有什么问题,我该如何让它工作?
谢谢。
这应该会让您走上正确的道路。如果它不起作用并且不是一个明显的错误,那么我怀疑您可能没有正确的所有文件路径......我应该指出,编写文件从减少开销中受益于线程而不是进程。文件 I/O 应该释放 GIL,这样您就可以从加速中获益(如果您一次复制多行,效果会更好。)也就是说,如果您只是复制文件,您应该只使用 shutil.copy
或 shutil.copy2
from concurrent.futures import ProcessPoolExecutor, wait
from pathlib import Path
import sys
def translate(filename):
print(filename)
with open(filename, "r") as f, open(filename + ".x", , "w") as g:
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():
futures.append(executor.submit(translate, "filelist/" + filename))
wait(futures) #simplify waiting on processes if you don't need the result.
for future in futures:
if future.excpetion() is not None:
raise future.exception() #ProcessPoolEcecutors swallow exceptions without telling you...
print("done")
if __name__ == "__main__":
main(sys.argv[1])
我有一个简单的任务。对于大量文件,特定功能需要 运行。这个任务可以很容易地并行化。
这是工作代码:
# filelist is the directory containing two file, a.txt and b.txt.
# a.txt is the first file, b.xt is the second file
# I pass a file that lits the names of the two files 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])
但是,没有创建新文件,即文件夹不包含 a.txt.x 和 b.txt.x 文件。
上面的代码有什么问题,我该如何让它工作?
谢谢。
这应该会让您走上正确的道路。如果它不起作用并且不是一个明显的错误,那么我怀疑您可能没有正确的所有文件路径......我应该指出,编写文件从减少开销中受益于线程而不是进程。文件 I/O 应该释放 GIL,这样您就可以从加速中获益(如果您一次复制多行,效果会更好。)也就是说,如果您只是复制文件,您应该只使用 shutil.copy
或 shutil.copy2
from concurrent.futures import ProcessPoolExecutor, wait
from pathlib import Path
import sys
def translate(filename):
print(filename)
with open(filename, "r") as f, open(filename + ".x", , "w") as g:
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():
futures.append(executor.submit(translate, "filelist/" + filename))
wait(futures) #simplify waiting on processes if you don't need the result.
for future in futures:
if future.excpetion() is not None:
raise future.exception() #ProcessPoolEcecutors swallow exceptions without telling you...
print("done")
if __name__ == "__main__":
main(sys.argv[1])