Use MEventMessage::addEventCallback with the ilde event, remeber to delete yourself in the event tough or youll get a infite loop.
Simple and bad (you dont need globals but its fast to do, casues a leak warning tough for understandable reasons) python example (port yourself):
import maya.OpenMaya as om
global id
def idleCB(*args):
print args
om.MMessage.removeCallback(id)
id = om.MEventMessage.addEventCallback("idle", idleCB, None)