Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using notifications #13

Open
jwise-acton opened this issue Jan 11, 2023 · 6 comments
Open

Using notifications #13

jwise-acton opened this issue Jan 11, 2023 · 6 comments

Comments

@jwise-acton
Copy link

I'm looking for a basic working example using notifications to communicate between two tasks. In the simplest case, I want to send an incrementing integer, as the notification value, from one task to another task once a second.

Thanks for any help!
...Jeff

@Rybec
Copy link
Owner

Rybec commented Jan 11, 2023

https://github.com/Rybec/pyRTOS#notification-examples

I just added that. Please let me know how that works for you, so I can either close the issue or fix whatever I got wrong! If it is not detailed enough, please let me know.

Note that if all you want to do is unblock a task once per second, it might be better to use a yield [timeout(1)] in that task. If you need greater stability though (as that will lose a little bit of time each iteration), using a notification in this way is certainly a better option, if you write you timing code correctly. In this case, you could use a service routine for handling the timing and sending the notifications, to optimize response time.

@jwise-acton
Copy link
Author

jwise-acton commented Jan 12, 2023 via email

@Rybec
Copy link
Owner

Rybec commented Jan 12, 2023

There are several ways to "inform" tasks about the existence of other tasks. The mailbox mechanic should work exactly as you describe it for this. Another option is global variables. This is commonly used with FreeRTOS and other embedded RTOS applications. In Python, you would create your global task variables (empty, since the task instances don't exist yet) first. Then you would create your task functions, with the global x line at the top, to bring the global variable into local scope. As long as you assign the tasks to the empty global variables before starting the tasks, this should work very smoothly. Yet another option is to make a global singleton class that all of the task functions use global to bring into local scope, and put everything that tasks need to know about there.

In my experience, just using global variables tends to be the most elegant option, if you only have one instance of each task. This eliminates the startup cost of passing in through the mailbox, and it makes it very clear what tasks interact with other tasks.

If you are going to create multiple instances of a task and need each instance to communicate with a different task, the mailbox method is the best choice. It can also be a good choice when there is some reason you can't do things in the order necessary for just using globals directly.

There's actually also a method that can work really well, if your tasks are handled in their own modules, by making the module level globals. This is kind of advanced, and it might make your code harder to read. If you want to know though, ask and I'll explain how to do it.

(Please don't use the singleton method. There might be places where it is appropriate, but singletons almost always make code way harder to read, and they tend to encourage terrible coding practices.)

Maybe I should add an "intertask communication" section to the documentation discussing this topic...

@Rybec
Copy link
Owner

Rybec commented Jan 12, 2023

Ok, so I've written up a new section in the examples section:

https://github.com/Rybec/pyRTOS/blob/main/README.md#communication-setup-examples

This has examples of how to get references to another task into a task. I hope this helps!

@jwise-acton
Copy link
Author

jwise-acton commented Jan 13, 2023 via email

@jwise-acton
Copy link
Author

jwise-acton commented Jan 13, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants