1import json
2
3import pytest
4from filelock import FileLock
5
6
7@pytest.fixture(scope="session")
8def session_data(tmp_path_factory, worker_id):
9 if worker_id == "master":
10 # not executing in with multiple workers, just produce the data and let
11 # pytest's fixture caching do its job
12 return produce_expensive_data()
13
14 # get the temp directory shared by all workers
15 root_tmp_dir = tmp_path_factory.getbasetemp().parent
16
17 fn = root_tmp_dir / "data.json"
18 with FileLock(str(fn) + ".lock"):
19 if fn.is_file():
20 data = json.loads(fn.read_text())
21 else:
22 data = produce_expensive_data()
23 fn.write_text(json.dumps(data))
24 return data
25