def store_runtime(a_task: asyncio.Task): a_bench = task_map[a_task] if result_file.exists(): with result_file.open('r+') as fp: try: original: Dict[str, Any] = json.load(fp) if 'runtime' in original: original['runtime'][a_bench.identifier] = a_bench.runtime else: original['runtime'] = {a_bench.identifier: a_bench.runtime} fp.seek(0) json.dump(original, fp, indent=4) except json.decoder.JSONDecodeError: fp.seek(0) fp.truncate() json.dump({'runtime': {a_bench.identifier: a_bench.runtime}}, fp, indent=4) else: with result_file.open('w') as fp: json.dump({'runtime': {a_bench.identifier: a_bench.runtime}}, fp, indent=4) a_task.remove_done_callback(store_runtime)