timegen¶
Time source abstraction.
This module provides TimeGen, the time source used by the scheduler (and other time-related components) to get the current time and to wait on condition variables. It can be subclassed to provide custom - e.g. simulated - time sources.
TimeGen
¶
A time source for time-driven components.
The default implementation uses the system time and condition variable wait semantics. Subclassing TimeGen allows to control the flow of time - e.g. for testing time-based logic without real delays.
Example
class ShiftedTimeGen(timegen.TimeGen):
def now(self):
return super().now() + 3600 # one hour in the future
Source code in py_misc_utils/timegen.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
now()
¶
Returns the current time.
Returns:
| Type | Description |
|---|---|
|
The current time, in seconds since the epoch. |
Source code in py_misc_utils/timegen.py
25 26 27 28 29 30 31 | |
wait(cond, timeout=None)
¶
Waits on a condition variable until it is notified - or timeout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cond
|
The threading condition variable to wait on. The caller must hold its lock. |
required | |
timeout
|
An optional maximum number of seconds to wait. If None, the wait blocks until the condition variable is notified. |
None
|
Returns:
| Type | Description |
|---|---|
|
The result of the underlying condition variable wait operation: True |
|
|
if it has been notified, False if the timeout expired. |
Source code in py_misc_utils/timegen.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |