Product SiteDocumentation Site

3.7.2. Python Specific Issues

A pcp client may be written in the python language by making use of the python bindings for PMAPI. The bindings use the python ctypes module to provide an interface to the PMAPI C language data structures. The primary imports that are needed by a client are:
  • cpmapi which provides access to PMAPI constants
    import cpmapi as c_api
  • pmapi which provides access to PMAPI functions and data structures
    from pcp import pmapi
  • pmErr which provides access to the python bindings exception handler
    from pcp.pmapi import pmErr
  • pmgui which provides access to PMAPI record mode functions
    from pcp import ppmgui
Creating and destroying a PMAPI context in the python environment is done by creating and destroying an object of the pmapi class. This is done in one of two ways, either directly:
    context = pmapi.pmContext()
or by automated processing of the command line arguments (refer to the pmGetOptions man page for greater detail).
    options = pmapi.pmOptions(...)
    context = pmapi.pmContext.fromOptions(options, sys.argv)
Most PMAPI C functions have python equivalents with similar, although not identical, call signatures. Some of the python functions do not return native python types, but instead return native C types wrapped by the ctypes library. In most cases these types are opaque, or nearly so; for example pmid:
   pmid = context.pmLookupName("mem.freemem")
   desc = context.pmLookupDescs(pmid)
   result = context.pmFetch(pmid)
   ...
See the comparison of a standalone C and python client application in Example 3.23, “PMAPI Error Handling”.