A robot cell can leave a camera inspection waiting on a delayed response. The inspection service needs a deadline for the wait and a timestamp that a technician can line up with the camera, controller, and production log. Python offers a monotonic clock for measuring the interval and a wall clock for recording when events appeared to happen. Assign each clock an explicit role so a wall clock correction cannot silently extend the wait.
The example below is an injected software fixture. It models a camera reply that misses a 150 millisecond inspection budget. The calculation ran in Python 3.14.7 without changing the computer's time settings or operating any equipment. Its numbers illustrate the decision rule and provide no timing estimate for a specific camera, network, or robot cell.
Set the deadline once
Take a monotonic reading when the request enters the part of the workflow covered by the budget. Add the budget in nanoseconds to get the deadline. Take later readings from the same clock and compare them with that deadline.
from time import monotonic_ns
budget_ns = 150_000_000
deadline_ns = monotonic_ns() + budget_ns
def remaining_ns():
return max(0, deadline_ns - monotonic_ns())
This version defines the cutoff as inclusive. At the deadline, the remaining budget is zero. The function returns a value for a caller to use when it waits for the reply. It does not interrupt a blocking device call. Pass the remaining budget to the mechanism that actually waits and confirm the units that mechanism accepts. For an interface expecting seconds, divide the remaining integer nanoseconds by one billion as close as possible to the call.
Give each retry the remaining budget
Suppose an inspection service first waits for a camera and then calls a result parser. If the service gives both steps a fresh 150 milliseconds, their combined wait can exceed the intended deadline. Compute the remaining time again at each step. Carry the same deadline through a retry when the retry belongs to the original inspection request.
Decide where the measurement begins. A deadline started at request receipt includes queue time. A deadline started when a camera worker takes the job measures a later interval. Both are reasonable contracts for different services. Give the resulting metric a name that makes the boundary clear, such as time from request receipt to result acceptance. Log the budget and the endpoint used for the decision so a replay measures the same interval.
Check both clocks in an injected example
In the fixture, the monotonic reading starts at 8.000 seconds and ends at 8.180 seconds. These are illustrative values in the clock's own coordinate system. The deadline is 8.150 seconds. The elapsed interval is 180 milliseconds, which puts the end reading 30 milliseconds after the deadline.
During the same interval, the simulated wall clock steps backward by two seconds. It advances by 180 milliseconds of elapsed time and then shifts back by 2,000 milliseconds, leaving a wall clock difference of minus 1,820 milliseconds. A check that compares that signed wall difference with the 150 millisecond budget would classify the request as still within budget. The monotonic comparison reaches the timeout decision.
Both readings describe the same example. One captures the progress of the wait. The other records what happened to the system clock between observations. This is why an operator can see wall timestamps in an incident log while the inspection service still uses elapsed time to decide when to stop waiting.
Store the clock context in the receipt
Record a UTC timestamp with a request ID for correlation. Record the monotonic elapsed interval, budget, and deadline outcome as separate fields. When the monotonic reading itself is needed for a diagnostic, preserve enough context to tell which host and run produced it. Python documents an unspecified reference point for this clock. Comparing raw readings from different machines requires a separate clock alignment design. For the same reason, a monotonic reading has no direct conversion to a calendar date.
Use integer nanoseconds for subtraction and deadline comparisons. An integer timestamp expressed in nanoseconds describes the unit of the API. The underlying timer can have a coarser resolution. Python exposes clock information through get_clock_info, including reported resolution and clock implementation. The local test recorded those fields for its own runtime only. It made no claim about camera timestamp precision or the latency of the operating system.
Test the boundary and the reply sequence
Exercise a reply just before the deadline, one at the deadline, and one after it. Feed the clock values into the decision function so the test runs without sleep or scheduler variation. Add a separate test for a wall clock adjustment and verify that it does not change the timeout outcome. A retry test can also show that the absolute deadline survives another attempt.
A late reply may still arrive after a timeout has been recorded. The inspection service needs an explicit policy for that event. It can retain the late reply as diagnostic evidence and match it with the original request ID. It also needs to decide whether the result may still change the inspection record. Write that decision separately from clock selection. A reliable timeout receipt shows the request boundary, the elapsed interval, and the state transition that followed it.
Sources checked November 28, 2026
Python Standard Library, time.monotonic_ns and get_clock_info
https://docs.python.org/3/library/time.html#time.monotonic_ns
https://docs.python.org/3/library/time.html#time.get_clock_info
Python Enhancement Proposal 418, clock selection and use cases
https://peps.python.org/pep-0418/
Python Enhancement Proposal 564, integer nanosecond APIs
https://peps.python.org/pep-0564/
Illustration. AI-generated concept based on the injected clock readings and the inspection deadline.