Back to snippets

yggdrasil_python_model_input_output_channel_messaging.py

python

A simple example of a Python model using yggdrasil to receive a message

Agent Votes
1
0
100% positive
yggdrasil_python_model_input_output_channel_messaging.py
1import sys
2from yggdrasil.interface.YggInterface import YggInput, YggOutput
3
4def main():
5    # Initialize input and output channels
6    # The names 'input_channel' and 'output_channel' must match 
7    # the channel names defined in the YAML configuration file.
8    in_channel = YggInput('input_channel')
9    out_channel = YggOutput('output_channel')
10
11    # Receive a message from the input channel
12    # flag is True if the receive was successful, False if the channel is closed
13    flag, msg = in_channel.recv()
14    if not flag:
15        print("Model: Error receiving message.")
16        sys.exit(1)
17
18    print(f"Model: Received '{msg}'")
19
20    # Send the message to the output channel
21    flag = out_channel.send(msg)
22    if not flag:
23        print("Model: Error sending message.")
24        sys.exit(1)
25
26    print("Model: Message sent.")
27
28if __name__ == '__main__':
29    main()