I have Python tests written with pytest. These tests download test data and cache it as locally written files.
Now I am parallelising tests with pytest-xdist. How can I prevent parallel writes in test fixtures, as it will result in corrupted data and failed tests?
Ideally, only one test process needs to download the data and cache it as a file.
You can use filelock library to create a lock file for each download happening within test fixtures or tests.
Here is an example function
wait_other_writers()which accomplishes the goals above:For the example use of this function, see here.