Worker function that tries to acquire lock and increment counter.
(worker_id: int, lock_path: str, counter_file: str)
| 23 | |
| 24 | |
| 25 | def worker(worker_id: int, lock_path: str, counter_file: str) -> int: |
| 26 | """Worker function that tries to acquire lock and increment counter.""" |
| 27 | try: |
| 28 | with FileLock(lock_path): |
| 29 | # Critical section - read, increment, write counter |
| 30 | with open(counter_file) as f: # noqa: PTH123 |
| 31 | current_value = int(f.read().strip()) |
| 32 | |
| 33 | time.sleep(random.uniform(0.01, 0.1)) # Simulate some work |
| 34 | |
| 35 | with open(counter_file, "w") as f: # noqa: PTH123 |
| 36 | f.write(str(current_value + 1)) |
| 37 | |
| 38 | print(f"Worker {worker_id}: success") |
| 39 | return 0 |
| 40 | except Exception as e: |
| 41 | print(f"Worker {worker_id}: error: {e}") |
| 42 | return 1 |
| 43 | |
| 44 | |
| 45 | if __name__ == "__main__": |
no test coverage detected