Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
import fcntl
|
||||
import os
|
||||
import pathlib
|
||||
import time
|
||||
import typing
|
||||
|
||||
CPUS_PER_SLOT = 4
|
||||
@@ -26,12 +25,16 @@ def slot_folder(env: typing.Mapping[str, str], home: pathlib.Path) -> pathlib.Pa
|
||||
return pathlib.Path(state) / "run-heavy"
|
||||
|
||||
|
||||
def taken_slot(folder: pathlib.Path, slots: int) -> tuple[int, int] | None:
|
||||
"""The number and open file of the lowest slot this call locked; None while every slot is held. Raises OSError where a slot file cannot be opened or locked."""
|
||||
# Locks an open file, as fcntl.flock does.
|
||||
Lock = typing.Callable[[int, int], None]
|
||||
|
||||
|
||||
def taken_slot(folder: pathlib.Path, slots: int, lock: Lock) -> tuple[int, int] | None:
|
||||
"""The number and open file of the lowest slot this call locked, through lock; None while every slot is held. Raises OSError where a slot file cannot be opened or locked."""
|
||||
for slot in range(slots):
|
||||
fd = os.open(folder / f"slot-{slot}", os.O_RDWR | os.O_CREAT, 0o644)
|
||||
try:
|
||||
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
lock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except BlockingIOError:
|
||||
os.close(fd)
|
||||
continue
|
||||
@@ -42,10 +45,10 @@ def taken_slot(folder: pathlib.Path, slots: int) -> tuple[int, int] | None:
|
||||
return None
|
||||
|
||||
|
||||
def waited_slot(folder: pathlib.Path, slots: int, say: typing.Callable[[], None], sleep: typing.Callable[[float], None] = time.sleep) -> tuple[int, int]:
|
||||
"""The number and open file of a slot this call locked, once one frees. While every slot is held, it calls say once and tries again each POLL_SECONDS."""
|
||||
def waited_slot(folder: pathlib.Path, slots: int, say: typing.Callable[[], None], sleep: typing.Callable[[float], None], lock: Lock) -> tuple[int, int]:
|
||||
"""The number and open file of a slot this call locked, through lock, once one frees. While every slot is held, it calls say once and tries again each POLL_SECONDS."""
|
||||
said = False
|
||||
while (taken := taken_slot(folder, slots)) is None:
|
||||
while (taken := taken_slot(folder, slots, lock)) is None:
|
||||
if not said:
|
||||
say()
|
||||
said = True
|
||||
|
||||
Reference in New Issue
Block a user