2

To make a somewhat long explanation rather simple for someone who's not fully into my project as me; I'm trying to find a way to detect a global variables that change in Python 2.7.

I'm trying to send updated to another device who registers them.

To reduce traffic and CPU load, instead of opting for a periodic update message, I was thinking of only sending an update message when a variable changes, and I might be tired right now but I don't know how I can detect a variable that changes.

Is there a library or something I can take advantage of?

Thank you!

3
  • 3
    There should only be a finite number of places which change the variable. Create a function which makes a change to the variable and at the same time sends the update. Make every place that wants to change the variable call that function instead. Encapsulation is the keyword here. Commented Feb 27, 2017 at 10:30
  • Change the name of the variable too so you get an error for anythign not going through the new function Commented Feb 27, 2017 at 10:32
  • This answer has some clues: stackoverflow.com/questions/7540443/… and mentions a notify package: download.gna.org/py-notify/reference/index.html Commented Feb 27, 2017 at 12:03

1 Answer 1

0

You could use the Pub/Sub feature of Redis if this kind of behaviour is typical in your codebase. https://redis.io/topics/pubsub.
Every time your variable changes, you publish this event on a channel.
For example let's call the channel variableUpdates.
The devices that depend on your variable value subscribe to the channel variableUpdates.
Every time when your variable changes you publish this event on the channel variableUpdates.
When this happens, your listeners get notified of this event, read the new variable value and use it in their own context.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.