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
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.