res = {} with ThreadPoolExecutor() as e: fs = {} for root, dirs, files in os.walk('.'): for file in files: fs[e.submit(upload_by_file, os.path.join(root, file))] = os.path.splitext(file)[0] for f in as_completed(fs): try: data = f.result() except Exception as exc: print(f'generated an exception: {exc}, {fs[f]}') else: res[fs[f]] = data print(fs[f], data) with open('test.json', 'w+', encoding='utf-8') as f: json.dump(res, f) upload_by_file 就是一个上传文件的函数
测试一共 249 个文件, 通过输出可以知道 249 个文件都已经上传完毕, 但是程序并没有执行到保存 json 文件. 而是假死了.
与文件 IO 相关, 代码这么写有时候就会卡住, 而有时候不会.
环境是 Windows 10, Python 3.9
