Update (2016-06-03): a recent PR added support for throttled logging in Python. See wiki/rospy/Overview/Logging - Logging Periodically.
Answering my own question:
It would seem this is rather difficult to implement in Python. The ROS_*_THROTTLE(..)
macro can exploit some features of C/C++ (variable scoping, static variables): state information is inserted for every individual invocation of the macro (see ros/console.h#L422-433). This makes it possible to easily support multiple calls with different period
arguments, without resorting to for instance __FILE__ __LINE__
trickery (which wouldn't work anyway).
Python does not support this, and would require a different approach. After some experimentation, adding runtime, per-invocation state information seems hard to implement, at least in a way supported by the many runtimes in existence and/or Python 3+. One approach could be to use a (object_id, function_name, line_nr)
tuple to uniquely identify an invocation. Unfortunately it seems impossible to retrieve this information without the use of functionality not supported everywhere (inspect
and / or sys._getframe()
). This would also shift the responsibility of keeping track of previous invocations to the log*throttle(..)
implementations.