nspyre.data.sink
Module Contents
Classes
For sinking data from the |
- class nspyre.data.sink.DataSink(name, addr='localhost', port=DATASERV_PORT, auto_reconnect=False)
For sinking data from the
DataServer
.data
can be used to directly access the python object pushed by the source. E.g.:First, start the data server:
$ nspyre-dataserv
from nspyre import DataSink, DataSource with DataSource('my_dataset') as src: src.push('Data!') with DataSink('my_dataset') as sink: sink.pop() print(sink.data)
Alternatively, if the data pushed by the source is a dictionary, its values can be accessed as if they were attributes of the sink, e.g.:
from nspyre import DataSink, DataSource with DataSource('my_dataset') as src: data = { 'some_data': 1, 'some_other_data': 'a string' } src.push(data) with DataSink('my_dataset') as sink: sink.pop() print(sink.some_data) print(sink.some_other_data)
- Parameters:
name (str) – Name of the data set.
addr (str) – Network address of the data server.
port (int) – Port of the data server.
auto_reconnect (bool) – If True, automatically reconnect to the data server if it is disconnected. Otherwise raise an error if connection fails.
- data: Any
The object pushed by the
DataSource
.
- start()
Connect to the data server.
- stop()
Disconnect from the data server.
- pop(timeout=None)
Block waiting for an updated version of the data from the data server. Once the data is received, the internal
data
attribute will be updated and the function will return.Typical usage example:
First start the data server from the console on machine A:
$ nspyre-dataserv
Run the python program on machine A implementing the experimental logic:
# machine A: "source"-side code # running on the same machine as the data server, in the same or a # different process from nspyre import DataSource import numpy as np # connect to the data server and create a data set, or connect to an # existing one with the same name if it was created earlier with DataSource('my_dataset') as source: # do an experiment where we measure voltage of something as a # function of frequency n = 100 frequencies = np.linspace(1e6, 100e6, n) voltages = np.zeros(n) for f in frequencies: signal_generator.set_frequency(f) voltages[i] = daq.get_voltage() # push most up-to-date data to the server source.push({'freq': frequencies, 'volts': voltages})
Then run the python program on machine B implementing the data plotting:
# machine B: "sink"-side code # running on a different machine as the source / data server from nspyre import DataSink # connect to the data set on the data server # IP of data server computer = '192.168.1.50' with DataSink('my_dataset', '192.168.1.50') as sink: while True: # block until an updated version of the data set is available sink.pop(): # sink.freq and sink.volts have been modified # replot the data to show the new values my_plot_update(sink.freq, sink.volts)
- Parameters:
timeout – Time to wait for an update in seconds. Set to
None
to wait forever.- Raises:
TimeoutError – A timeout occured.
RuntimeError – The sink isn’t properly initialized.