endurox.tpext_addpollerfd

endurox.tpext_addpollerfd(fd: int, events: int, ptr1: object, func: object) None

Monitor file descriptor in XATMI server main dispatcher. This allows the main thread of the server process to select either a service call or perform callback if file descriptor fd has events arrived. Events monitor are ones which are passed to poll() unix call, and shall be passed in events field, such as select.POLLIN. tpext_addpollerfd() can be only called when XATMI server has performed the init, i.e. outside of the tpsvrinit().

Function is not thread safe. This function applies to ATMI servers only.

tpext_addpollerfd example
import sys, os, select
import endurox as e

outx = None
path = "/tmp/tmp_py"

def cb(fd, events, ptr1):
    e.tplog_info("fd %d got event %d" % (fd, events));
    # process the events...
    return 0

# use b4poll() callback to activate the fd callback
def b4poll():
    e.tpext_addpollerfd(outx, select.POLLIN, None, cb)
    e.tpext_delb4pollcb()
    return 0

class Server:

    def tpsvrinit(self, args):
        os.mkfifo( path, 0O644 )
        outx = os.open(path, os.O_NONBLOCK | os.O_RDWR)
        e.tpext_addb4pollcb(b4poll)
        return 0

    def tpsvrdone(self):
        global outx
        global path
        os.close(outx)
        os.remove(path) if os.path.exists(path) else None
        e.userlog('Server shutdown')

if __name__ == '__main__':
    e.tprun(Server(), sys.argv)

For more details see tpext_addpollerfd(3) C API call.

Raises:

AtmiException

Following error codes may be present:
TPEMATCH - fd is already registered with callback.
TPEPROTO - Called from invalid place, e.g. tpsvrinit() func.
TPESYSTEM - System error occurred.
TPEOS - Operating system error occurred.

Parameters:
  • fd (int) – File descriptor to monitor.

  • events (uint32) – Bitnmask of select events. In case if using kqueue, convert flags from signed value to unsigned, e.g. select.KQ_FILTER_READ + 2^32.

  • ptr1 (object) – Custom object passed to callback.

  • func (object) – Callback func used for notifying for events occurred on fd. Function signature must accept signature of “(fd, events, ptr1)”. Where fd is file descriptor, events is poll() events occurred on fd, ptr1 is custom pointer passed when tpext_addpollerfd() was called.