Using Callbacks

Callbacks can be used instead of or in addition to busy-wait loops to get notification that an asynchronous OpenAPI function has completed the requested action.

Callbacks are required for handling unpredictable communications from the DBMS, such as various kinds of trace messages (I/O, logging, etc.) and notifications of database events (DBEvents).

When a callback is specified using the IIAPI_GENPARM structure of an OpenAPI asynchronous function control block, the gp_callback attribute is the address of the callback function, and gp_closure can be a pointer to a an optional value that will be passed to the callback as an argument.

Closure is the means by which an application passes information to the callback function.

Callback functions are expected to take two arguments. The first is expected to be a pointer to the closure, and the second is expected to be a pointer to an IIAPI_GENPARM structure.

Callbacks can’t return a value, but if passed a pointer to a structure as the closure they can update its contents. They can also update globals.

Note

This example makes use of the IIapi_getCallbackPtr() and IIapi_getClosure() helper functions provided by pyngres. They are not part of the Actian OpenAPI.

Tip

Refer to the ctypes documentation for additional explanation of callbacks.

Download

 1import pyngres as py
 2
 3...
 4
 5# create example_callback() 
 6@py.IIapi_callback
 7def iiapi_trace(trace_block,parmBlock=None):
 8    '''trace-message callback'''
 9
10    # get the trace_block argument as a Python IIAPI_TRACEPARM instance
11    trp = py.IIapi_getClosure(trace_block,py.IIAPI_TRACEPARM)
12    # extract the tr_message member containing a line of trace info
13    tr_message = trp.tr_message
14    ...
15
16# install iiapi_trace() as the callback to handle trace-messages
17tracer = py.IIapi_getCallbackPtr(iiapi_trace)
18sep = py.IIAPI_SETENVPRMPARM()
19sep.se_envHandle = envHandle
20sep.se_paramID = py.IIAPI_EP_TRACE_FUNC
21sep.se_paramValue = tracer
22py.IIapi_setEnvParam(sep)

Download