single thread applications
You can execute a function as a single thread application by calling run(). Look at several examples of run() in IoTPy/examples/op/examples_map_e.py, IoTPy/examples/op/examples_map_w.py, IoTPy/examples/merge/examples_merge.py, IoTPy/examples/split/examples_split.py, and IoTPy/examples/sink/examples_sink.py.
Look at the make_echo example. You could run the code in the following way:
def test_echo(): # Step 1: Declare streams. spoken = Stream('spoken')' # Step 2: Define the functions of the application. heard = make_echo(spoken, D=1, A=0.5) print_stream(heard) # Step 3: Insert data into input streams. spoken.extend([64, 32, 16]) # Step 4: Call run() run()
The first step is to create streams used by the application. The second step is to define the functions that execute the application. Recall that the parameters D and A of the function make_echo are the delay and attenuation factor, and these are set to 1 and 0.5 in this example. The third step is to insert data into the input streams of the application. In this example, the source of data is the sound that is spoken which is captured in the spoken stream. In this toy example, we have extended spoken with a list of only 3 values. Later we will see how to extend the stream with data from .wav files or from a microphone. The fourth step is to call run(). In this example, the call to run() executes the application which prints the heard stream to the screen. The output to the screen will be 64, 64.0, 48.0, with each value on a separate line.
You can change the values of the streams and execute run() multiple times within the same program. For example, within test_echo you can extend the stream, spoken, with the list [64, 32, 16] and then print the output, and then extend it again with the list [8, 4, 2, 1] and then print the output, and finally extend the stream with values [0, 0, 0, 0] and then execute run() again. The following code illustrates multiple executions of run().
def test_echo(): spoken = Stream('spoken') heard = make_echo(spoken, D=1, A=0.5) print_stream(heard) # Put data into input stream spoken.extend([64, 32, 16]) print('first step') run() # Put additional data in stream. spoken.extend([8, 4, 2, 1]) print('second step') run() # Put additional data in stream. spoken.extend([0, 0, 0, 0]) print('third step') run()
The output will be: ‘first step’, and then 64, 64.0, 48.0 with each value on a separate line, then ‘second step’, followed by 32.0, 20.0, 12.0, 7.0 on separate lines, and then ‘third step’ followed by 3.5, 1.75. 0.875, 0.4375 on separate lines.
Executing run() after reading sections of a file
You can execute run() after putting one or more elements from a set of files on to streams. The following code executes run() after reading each line of a file of integers.
def test_source_file(filename): s = Stream('s') print_stream(s) with open(filename, 'r') as input_file: for line in input_file: s.append(int(line)) run()
Executing run() in this way is helpful to measure the time required to execute each step of an application.