|
| 1 | +import asyncio |
1 | 2 | from datetime import datetime |
2 | 3 | from os.path import isfile |
3 | 4 | from typing import Any, Optional, Type, TypedDict, TypeVar, Union |
|
7 | 8 |
|
8 | 9 | from pyhilo.const import LOG |
9 | 10 |
|
| 11 | +lock = asyncio.Lock() |
| 12 | + |
10 | 13 |
|
11 | 14 | class TokenDict(TypedDict): |
12 | 15 | access: Optional[str] |
@@ -103,10 +106,11 @@ async def set_state( |
103 | 106 | :type state: ``StateDict`` |
104 | 107 | :rtype: ``StateDict`` |
105 | 108 | """ |
106 | | - current_state = await get_state(state_yaml) or {} |
107 | | - merged_state: dict[str, Any] = {key: {**current_state.get(key, {}), **state}} # type: ignore |
108 | | - new_state: dict[str, Any] = {**current_state, **merged_state} |
109 | | - async with aiofiles.open(state_yaml, mode="w") as yaml_file: |
110 | | - LOG.debug("Saving state to yaml file") |
111 | | - content = yaml.dump(new_state) |
112 | | - await yaml_file.write(content) |
| 109 | + async with lock: # note ic-dev21: on lock le fichier pour être sûr de finir la job |
| 110 | + current_state = await get_state(state_yaml) or {} |
| 111 | + merged_state: dict[str, Any] = {key: {**current_state.get(key, {}), **state}} # type: ignore |
| 112 | + new_state: dict[str, Any] = {**current_state, **merged_state} |
| 113 | + async with aiofiles.open(state_yaml, mode="w") as yaml_file: |
| 114 | + LOG.debug("Saving state to yaml file") |
| 115 | + content = yaml.dump(new_state) |
| 116 | + await yaml_file.write(content) |
0 commit comments