unaiverse.clock¶
This module provides the object clock that every part of a process uses as its single clock. The clock serves two purposes. First, it gives a wall time that agents on different machines agree on. At creation it asks a time server for the current time and from then on advances that value with the elapsed time of the local system clock. Second, it divides the life of the node into cycles. The node advances the clock by one cycle before each pass of its loop. The behaviors, the streams and the statistics use the cycle index and its timestamp as their common time base.
Every module imports the same object:
from unaiverse.clock import clock
t = clock.get_time() # seconds since the Unix epoch, synchronized
ms = clock.get_time_ms() # the same in integer milliseconds
iso = clock.get_time_as_string() # "2026-11-23T10:15:02.418+00:00"
The Node creates the clock in its constructor with clock.create(min_delta=clock_delta). Its argument clock_delta defaults to 1/25 seconds and allows at most 25 cycles per second. When no time server answers, the node asks on the terminal whether to continue with the local time. A program that reads any attribute of clock before a node exists gets a clock created on the spot from the local time. Its min_delta is -1 and every next_cycle() on it advances at once.
Time servers¶
At creation the clock tries the time pools pool.ntp.org, north-america.pool.ntp.org, asia.pool.ntp.org and europe.pool.ntp.org in this order. When none answers (a firewall that blocks the protocol, for example), it sends an HTTPS HEAD request with a timeout of 5 seconds to a public web server and reads the Date header. The header has a resolution of one second and the clock adds half of the round-trip time to it. When this request fails too, the creation raises ValueError("Unable to get the initial time ..."). The clock queries the server only once and from then on adds the elapsed local time to the time it received.
Methods reached through clock¶
The object clock forwards every attribute it does not have to the clock it wraps. The methods below therefore read as clock.<method>().
get_time(passed=False) -> float¶
Returns the synchronized wall time in seconds since the Unix epoch with microsecond precision. With passed=True it returns the seconds elapsed since the clock was created.
get_time_ms(monotonic=False) -> int¶
Returns the synchronized wall time in integer milliseconds. With monotonic=True the result is strictly greater than the last value returned by this method. The call blocks the thread and sleeps in steps of 0.1 milliseconds until the time has moved past that value. The statistics and the badges use this method for their timestamps.
get_time_as_string() -> str¶
Returns the synchronized wall time as an ISO 8601 string in UTC with milliseconds (for example "2026-11-23T10:15:02.418+00:00").
next_cycle() -> bool¶
Advances to the next cycle and records its timestamp. When the clock has a positive min_delta, the call returns False and changes nothing until min_delta seconds have passed since the timestamp of the current cycle. The node calls it in a loop before each pass and waits until it returns True. With min_delta of 0 or less every call advances. Two calls in that mode within the same microsecond record the same timestamp and raise ValueError.
get_cycle() -> int¶
Returns the index of the current cycle. The index is -1 before the first call of next_cycle() and 0 after it.
get_cycle_time() -> float¶
Returns the timestamp of the current cycle in seconds since the epoch. The value is -1. before the first cycle. Streams stamp every sample they produce with this value. All the samples of one cycle therefore share a timestamp.
time2cycle(timestamp, delta=None) -> int¶
Converts a timestamp in seconds to a cycle index. With delta greater than 0 the result is the current cycle minus the number of whole delta intervals between timestamp and now. Without delta the method looks the timestamp up among the recorded cycles and returns the index of the cycle with exactly that timestamp. A timestamp after the last cycle gives the index of the next cycle (the number of cycles recorded so far). Every other timestamp gives -1 and so does any timestamp on a clock without cycles. Streams use it to find the samples produced since a given time.
cycle2time(cycle, delta=None) -> float¶
Converts a cycle index to a timestamp. With delta greater than 0 the result is cycle * delta, a value measured from zero and not from the first cycle. Without delta the result is the recorded timestamp of cycle, -1. for a negative index and IndexError for an index past the last cycle.
Slow mode¶
run_slower() raises the minimum time between cycles to at least Custom.SLOW_CLOCK_DELTA (2 seconds). run_natural_speed() restores the min_delta given at creation. is_slowed_down() returns True when the current min_delta differs from the one given at creation. On a clock created with a min_delta of 2 seconds or more it therefore always returns False. The node slows the clock down when it has exchanged nothing on the network for Custom.SLOW_DOWN_CLOCK_AFTER seconds (10 by default) and has no open interaction. It restores the speed as soon as traffic or an interaction appears.
Attributes¶
min_delta is the current minimum time between cycles in seconds and cycle the current cycle index. The clock keeps the timestamp of every cycle in a list that grows by one entry per cycle and is never trimmed.
The class of the object clock: a holder of one clock that forwards every attribute access to it. The holder exists so that every module can import clock at import time while the node creates the real clock later with the right options. Accessing any attribute while the holder is empty creates a clock with current_time equal to the local UTC time. That clock keeps the default min_delta=-1 and asks no time server. A program does not create other instances of Clock.
create
¶
Creates a new clock and puts it in the holder in place of the previous one. The arguments go to the constructor of the clock.
| Argument | Type | Default | Meaning |
|---|---|---|---|
min_delta |
float |
-1 |
The minimum time between two cycles in seconds. A value of 0 or less makes every next_cycle() advance. |
current_time |
float |
-1. |
A positive value is the initial wall time in seconds since the epoch and no time server is asked. The default asks the time servers. |
Raises ValueError when no time server answers. The node calls it once in its constructor. Returns None.
clock.create(min_delta=0.5) # at most two cycles per second, synchronized
clock.create(min_delta=0.5, current_time=time.time()) # local time, no network access
set
¶
Puts the existing clock object existing_clock in the holder. Two holders can share one clock in this way. Returns None.
The instance of Clock that holds the clock of the process. Every module of the SDK imports this object.