drrossum
drrossum

Reputation: 614

cython/python: how to catch as exception an external function that "exits" instead of "returns"

I'm using Cython to interface an external C function. The function (a science code with 100k code lines) internally checks for invalid conditions and "exits" if they occur.

How can I get Cython to detect and raise an exception when the external function "exits" instead of "returns"?

Edit: user2864740 is right, cython actually exits. (I was running the function inside a python mulitprocessing.Process. To simplify I got rid of multiprocessing.) The question now is:

How can I prevent the "exit()" call from the external library to exit Cython (or Python, at a later point). A try-except construct doesn't catch this. Can this be caught as exception instead of the whole process dying?

Thanks!

Upvotes: 2

Views: 907

Answers (1)

user1915639
user1915639

Reputation: 257

There are guidelines for "Joining zombie processes" in the multiprocessing documentation. https://docs.python.org/3/library/multiprocessing.html#programming-guidelines If in the master process you find that the worker process is defunct, you can raise an exception.

Alternatively you could try registering an atexit handler from within the Cython code http://linux.die.net/man/3/atexit

A third idea is to use some kind of LD_PRELOAD library to override the exit function so that it raises a Python exception.

Upvotes: 2

Related Questions