Listening to changes in a C++ variable from python

Jeremy Bowers jerf at jerf.org
Sat May 7 10:19:18 EDT 2005


On Sat, 07 May 2005 07:16:58 -0700, lamthierry wrote:

> Is there some python method which can do the polling you are talking
> about? Or can you send me a link to some existing example?

Take a moment to go back to the basics. C++ is, in general, a simple
system. The *language* is complicated at times, but it does actually
strive for no unknown magic taking place. When you say "int i = 4", there
is some literal value in memory somewhere taking the value of 4. If you
say "i = 5", that value in memory now says five.

The reason you can't "listen" to this is quite literally because you RAM
does not have that ability (for good reason); there are no circuits that
are triggered when a value is changed. (I'm talking conventional RAM here,
with no hardware mapping or anything else special.)

Polling here simply means checking periodically. You don't need any
special functions or libraries, you just need to check periodically. Your
GUI system should have a system for creating timeouts, but without telling
me what that is, I can't give the code. (*I* may not be able to even so; I
have not used them all extensively enough to know about all the
schedulers.) Your timeout function should simply check the new value and
do the appropriate thing based on the new value.

So, there isn't really a "method", it's just combining your GUI scheduler
with a simple == or > or < or whatever else is appropriate in your case. 

If it takes more than about four or five lines to write the basic poller,
you're probably on the wrong track. (Correctly handling what the poller
sees may be more complicated, but the periodic checking should be simple.)

If you need more help (though I strongly suggest that you try to get the
scheduler to do something "two seconds from now" or something, and after
that it *should* be obvious what to do next), you'll need to include what
GUI toolkit you are using, and possibly the current Python code that you
are using to access the value you'd like the GUI to track. You may also
want to explain how you are connecting the apps; is the C++ part and the
Python part in one app in different threads, or are you using some other
communication form?

One warning: You might be tempted to use time.sleep(); don't do that. That
works OK in a Python program where only your code is running, but in any
GUI program the GUI itself is also running. Using time.sleep() will
completely stop *everything* during the duration of the sleep, completely
freezing the GUI as if it were locked up; that's why all GUIs have
schedulers, so they can keep running while your code waits.



More information about the Python-list mailing list