Urgent.News

What's breaking now, across thousands of outlets.

Tech

Use a monotonic deadline to stop starting new batch work

A time budget is easy to misread as a promise that a function will return before a particular instant. A loop that checks the time between jobs makes a narrower promise: it stops admitting new work when its budget expires. A job already running can still finish late. Making that boundary explicit gives you code you can test and a result you can explain. This example processes a finite sequence of…

A time budget is a tool for controlling the duration of a loop that processes a sequence of items. Unlike a promise that a function will return before a certain time, a time budget stops admitting new work once its budget expires. The loop can still finish processing the current item, but it will not start any new ones. A key aspect of implementing a time budget is choosing an appropriate clock.

Python's time.monotonic function provides a reliable clock that cannot go backward and is unaffected by changes to the system clock. By capturing this clock once and comparing later readings to it, you can enforce the budget without mixing monotonic deadlines with wall-clock readings. The example code demonstrates how to implement a time budget using a fake clock for testing purposes.

It shows that the budget does not prevent a running job from finishing, nor does it prevent the loop from progressing to the next item if it exists. However, the loop does not guarantee that the job will stop exactly at the budgeted time, as a hung work function could potentially block the batch. Other considerations include the possibility of the next item taking time to fetch or process, which could cause the loop to exceed the budget even before the current work begins.

The example also includes unit tests that cover various scenarios, such as empty input, zero budget, boundary expiration, and error propagation. These tests help verify that the time budget behaves as expected and provides useful assertions for different cases. When using the time budget, it is important to log the results using calendar timestamps for when the batch ran and elapsed differences to describe how much of the budget was consumed.

Keeping these values separate helps avoid confusion and ensures accurate reporting. While this small function is suitable for controlling the duration of local tasks, it may not be appropriate for hard deadlines, slow input producers, or cancellable work. In such cases, it is recommended to specify the requirements separately and not rely on this wrapper's promise.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Wednesday 18 November →