简体   繁体   中英

Prevent failing glog CHECK statement from terminating program

I have a C++ library which uses glog CHECK statements to asset various conditions on runtime. If a CHECK fails, glog terminates the program. My python program needs some functions from this C++ library, so I have written a simple C wrapper for those functions and compiled a dll, which I can load and use using ctypes .

The problem is, if a CHECK statement fails, the python program is immediately aborted and the user loses all unsaved work!

Instead I would like to handle cases where something went wrong in the library as exceptions in python. The C++ library is not written by me and rewriting it is not a good solution. How can I prevent my python program from being terminated when a glog CHECK statement fails in the C++ library called from it?

A quick solution is to run the function in a separate process. The function in the C++ library is heavy (~1 second), so this may be justified.

Minimal Python 3 example:

import ctypes
import multiprocessing

library = ctypes.cdll.LoadLibrary(...)

def RunFunction(params, queue):
    queue.put(library.function(*params))

if __name__ == '__main__': 
    params = ...
    queue = multiprocessing.Queue()
    p = multiprocessing.Process(target=RunFunction, args=(params, queue))
    p.start()
    p.join()
    if p.exitcode == 0:
        print("Function returned: {}".format(queue.get()))
    else:
        print("Non-zero exit code: {}".format(p.exitcode))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM