Episode 1: One Script, Two Processes
Run one script as two processes, an edge beside the device and a server that does the processing, and let the command line decide which half each one is.
Every pipeline so far ran in one process. A distributed one runs in two: an edge beside the amplifier, and a server that does the processing. They do not need two scripts. They run the same one, and the command line decides which half each process is.
Two terminals. The server starts first, then the edge:
python s8e1_one_script_two_processes.py --residency server --endpoint ws://localhost:8765 --bind loopback python s8e1_one_script_two_processes.py --residency edge --endpoint ws://localhost:8765
The server is a deployment, and needs a g.Pype Runtime licence. Without one its
start()refuses and names the reason. An edge never refuses for want of a licence; without one, what it writes carries the non-commercial mark.
LaunchConfig reads --residency, --endpoint and --bind, and the pipeline builds each source, sink and widget for the residency it finds.
17 # --residency, --endpoint and --bind, read from the command line.
18 config = gp.LaunchConfig.parse_args()
Both processes build the pipeline from code, and the two halves of a Link find each other by the node’s id. A node built in code gets a new id in every process, so without id="s8e1-eeg" the halves never pair. A serialized pipeline, the subject of Episode 2, carries its ids with it.
20 p = gp.Pipeline()
21 # Both processes build this pipeline, and the two halves of a Link
22 # find each other by the node's id. A node built in code gets a new
23 # id in every process, so give the source one.
24 source = gp.Generator(sampling_rate=250, channel_count=4,
25 signal_frequency=10, signal_amplitude=20.0,
26 id="s8e1-eeg")
27 band = gp.Bandpass(f_lo=8, f_hi=12)
28 out = gp.Collector()
29 p.connect(source, band)
30 p.connect(band, out)
p.chain_of(source) is the pipeline’s answer, and it differs by residency.
32 # What this process built for the source: the pipeline's answer.
33 built = [type(n).__name__ for n in p.chain_of(source).internal_nodes]
34 print(f"{config.residency}: the source is {built}")
Each process runs until Enter. A Collector touches no device, file or screen, so every process builds one, and only the server’s is fed: the edge’s stream leaves through its Link.
36 p.start()
37 input(f"{config.residency}: running. Press Enter to stop.\n")
38 p.stop()
39
40 # A Collector touches no device, file or screen, so every process
41 # builds one. Only the server's is fed: the edge's stream leaves
42 # through its Link.
43 result = out.result
44 got = "nothing" if result is None else f"{result.sample_count} samples"
45 print(f"{config.residency}: collected {got}")
46 p.close()
Each process prints what it built for the source and, once you press Enter, what its Collector received:
server: the source is ['Link', 'Sync']
edge: the source is ['Generator', 'Link', 'Sync']
edge: collected nothing
server: collected ... samples
The edge owns the world, the server the processing. The edge builds the Generator and a Link that sends its samples; the server builds only the receiving Link and the Sync, so it opens no device and could run on a machine without one. The Bandpass and the Collector touch no device, file or screen, so both processes build them, and only the server’s are fed. A sink or a widget is the other way round: the edge builds it, and the server only the Link that sends the stream back, so a script with a scope runs unchanged as a server. There, MainApp imports no Qt and builds no window, add_widget does nothing, and run() waits for Enter instead of showing one.
Start the server first. The edge relates its clock to the server’s once, as it starts and before its first sample, and waits up to 30 seconds for the server to answer. Here both run on one machine and share its clock, so this episode says nothing about how closely two machines agree.
ws:// is not encrypted, which is why the endpoint is localhost and the server binds loopback only: the samples on that socket are the recording. Across a network, use wss:// with a certificate.
In this episode, you learned how to:
run one script as the edge and as the server, from the command line
give a source an id so the two halves of its Link pair
read what each residency builds, and which process is fed
Proceed to Episode 2, where the pipeline travels as a serialized pipeline, and a node of your own with it.
Nodes in this episode: Generator, Bandpass, Collector
File s8e1_one_script_two_processes.py – View file on GitHub
1"""Season 8, Episode 1: one script, run as the edge and as the server.
2
3Start the server first, then the edge, each in its own terminal:
4
5 python s8e1_one_script_two_processes.py --residency server
6 --endpoint ws://localhost:8765 --bind loopback
7 python s8e1_one_script_two_processes.py --residency edge
8 --endpoint ws://localhost:8765
9
10The server half is a deployment: it needs a g.Pype Runtime licence.
11"""
12
13import gpype as gp
14
15if __name__ == "__main__":
16
17 # --residency, --endpoint and --bind, read from the command line.
18 config = gp.LaunchConfig.parse_args()
19
20 p = gp.Pipeline()
21 # Both processes build this pipeline, and the two halves of a Link
22 # find each other by the node's id. A node built in code gets a new
23 # id in every process, so give the source one.
24 source = gp.Generator(sampling_rate=250, channel_count=4,
25 signal_frequency=10, signal_amplitude=20.0,
26 id="s8e1-eeg")
27 band = gp.Bandpass(f_lo=8, f_hi=12)
28 out = gp.Collector()
29 p.connect(source, band)
30 p.connect(band, out)
31
32 # What this process built for the source: the pipeline's answer.
33 built = [type(n).__name__ for n in p.chain_of(source).internal_nodes]
34 print(f"{config.residency}: the source is {built}")
35
36 p.start()
37 input(f"{config.residency}: running. Press Enter to stop.\n")
38 p.stop()
39
40 # A Collector touches no device, file or screen, so every process
41 # builds one. Only the server's is fed: the edge's stream leaves
42 # through its Link.
43 result = out.result
44 got = "nothing" if result is None else f"{result.sample_count} samples"
45 print(f"{config.residency}: collected {got}")
46 p.close()