Publish a barrier file so a poller sees either no file or its content. Path.write_text creates and truncates before it writes, so a reader that polls for existence and then parses the content can observe the zero-byte window in between. Write next to the destination and rename into pla
(path: pathlib.Path, text: str)
| 151 | |
| 152 | |
| 153 | def publish_barrier_file(path: pathlib.Path, text: str) -> None: |
| 154 | """Publish a barrier file so a poller sees either no file or its content. |
| 155 | |
| 156 | Path.write_text creates and truncates before it writes, so a reader that |
| 157 | polls for existence and then parses the content can observe the zero-byte |
| 158 | window in between. Write next to the destination and rename into place. |
| 159 | """ |
| 160 | with tempfile.NamedTemporaryFile( |
| 161 | mode="w", |
| 162 | encoding="utf-8", |
| 163 | newline="\n", |
| 164 | dir=path.parent, |
| 165 | prefix=f".{path.name}.", |
| 166 | delete=False, |
| 167 | ) as temporary: |
| 168 | temporary.write(text) |
| 169 | temporary.flush() |
| 170 | temporary_path = pathlib.Path(temporary.name) |
| 171 | try: |
| 172 | os.replace(temporary_path, path) |
| 173 | except BaseException: |
| 174 | temporary_path.unlink(missing_ok=True) |
| 175 | raise |
| 176 | |
| 177 | |
| 178 | def start_suite( |
no test coverage detected