#! /usr/bin/env mpenv # # This script monitors a user supplied list of EPICS PVs for callbacks. # import time import Mp import MpCa if ( len(sys.argv) < 3 ): print "" print "Usage: mpca_monitor pvname ..." print "" sys.exit(0) #--------------------------------------------------------------------------- def pv_callback( callback, args ): pv = callback.pv print "'%s' = %s" % ( pv.pvname, str( pv.get_local() ) ) #--------------------------------------------------------------------------- pv_list = [] callback_list = [] for arg in sys.argv[2:]: # Create a PV for this PV name pv = MpCa.PV( arg ) print "Monitoring PV '%s'" % pv.pvname pv_list.append( pv ) # # FIXME: Calling caget() should not be necessary, but it # helps to make the PV channel connect. # value = pv.caget() if ( pv_list == [] ): print "No PV callbacks requested." sys.exit(0) for pv in pv_list: callback = pv.add_callback( MpCa.DBE_VALUE, pv_callback, None ) callback_list.append( callback ) del callback # # Loop forever waiting for callbacks. # # NOTE: The presence of the call to time.sleep(0.001) in the loop # is _VERY_ important. Calling time.sleep() causes the # Python Global Interpreter Lock to be released which allows # the EPICS-triggered Python callback to run. If the GIL is # not released on a regular interval by a call to something # like time.sleep(), then the EPICS-triggered Python callback # will often have to wait a very long time to acquire the GIL, # resulting in really bad performance for the EPICS callbacks. # while (True): MpCa.poll() if ( Mp.kbhit() ): break time.sleep(0.001) print "Exiting..."