简体   繁体   中英

Python detect changing (global) variable

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM