trader.daemon.sleeper module

Sleeping, as an injectable seam.

The daemon spends almost all of its life asleep, which would make its tests either slow or untrustworthy. Taking sleep and monotonic as arguments makes them instant and exact instead.

class trader.daemon.sleeper.RealSleeper(*, slice_seconds=1.0, sleep=<built-in function sleep>, monotonic=<built-in function monotonic>)[source]

Bases: object

Sleeps in short slices so a signal is noticed promptly.

One time.sleep(3600) would be interrupted by a signal on POSIX, but the handler only sets a flag — the sleep resumes for the remaining hour unless it is broken up. Slicing bounds the shutdown delay at slice_seconds regardless of how long the requested wait was.

Parameters:
  • slice_seconds (float)

  • sleep (Callable[[float], None])

  • monotonic (Callable[[], float])

sleep(seconds, shutdown)[source]

Sleep up to seconds, in slices, stopping early on shutdown.

Parameters:
Return type:

None

class trader.daemon.sleeper.Sleeper(*args, **kwargs)[source]

Bases: Protocol

Waits, but gives up early when shutdown is requested.

sleep(seconds, shutdown)[source]

Sleep up to seconds, returning early if shutdown is set.

Parameters:
Return type:

None