Timer Functions

class py4syn.utils.timer.Timer(timeout)[source]

A class to track elapsed time and timeouts. The timer class stores an initial time and a timeout value. These can be used to check or wait for a certain moment in time.

Examples

>>> from py4syn.utils.timer import Timer
>>> 
>>> def waitAcquisition(device, timeout=60):
...     timer = Timer(timeout)
...     while device.isAcquiring() and timer.check():
...         device.updateState()
...
...     if timer.expired():
...         raise TimeoutError()
...

The constructor receives a timeout parameter, which is saved and marks the initial time.

See: mark()

Parameters:
timeout : float

Timeout limit

check()[source]

Checks if the timeout has been expired by reading the current time value and comparing it against the initial time and the timeout. The expiration value is saved and can be read later with expired(). Returns True if the timeout hasn’t been reached, or False if the timer expired.

See: expired()

Returns:
`bool`
expired()[source]

Reads the cached expired value from check() or wait().

See: check(), wait()

Returns:
`bool`
mark()[source]

Resets the initial time value and resets the expired flag. After calling this method, timeout checking or waiting will use the new time value as the initial moment.

wait(file=None)[source]

Blocks until the timeout has expired, or, if the file parameter is not empty, until the file becomes ready with input data. The expiration value is saved and can be read later with expired(). Returns False if the timeout expired, or True if the file is ready for reading before the timer is expired.

This method only checks if the file is ready for reading, but it does not read anything from it. It will continuously return True while the file is not read and the timeout does not expire.

See: expired()

Returns:
`bool`