How to use librt function in Python?

I am trying to use a third-party .so, P4API.so which calls clock_gettime defined in librt.so and would like users of my script not to have to set LD_PRELOAD. Therefore, in the init .py file , I have:

import ctypes

librt = ctypes.cdll.LoadLibrary('librt.so')

This loads the library in order, but running the script still emits:

ImportError: /path/to/P4API.so: undefined symbol: clock_gettime

I tried:

__builtins__['clock_gettime'] = librt.clock_gettime

but it doesn’t work either.

How can I get P4API.so to recognize loaded librt?

+3
source share
1 answer

You need to download it using

 ctypes.CDLL('librt.so', mode=ctypes.RTLD_GLOBAL)

to make it available to other libraries.

+5
source

All Articles