简体   繁体   中英

how to stop and restart C++ code running from python with ctypes

I have a C++ code that I run and use it's functions from Python. My question is - how do I stop that code from running (like ctrl+c if it was in console) and then restart, but only the c++ part, the rest of python should work as before.

Here is how I use the c++ code:

from os.path import abspath, dirname, join
fname = abspath(inspect.getfile(inspect.currentframe()))
lib = ctypes.cdll.LoadLibrary(join(dirname(fname), 'shared_lib.so'))
func = lib.func
func.restype = None
func.argtypes= None

Is there a way to re-do it?

You could add a signal handler:

import signal
from os.path import abspath, dirname, join

def restart_server():
    # Restart code

signal.signal(signal.SIGTERM, restart_server)
fname = abspath(inspect.getfile(inspect.currentframe()))
lib = ctypes.cdll.LoadLibrary(join(dirname(fname), 'shared_lib.so'))
func = lib.func
func.restype = None
func.argtypes= None

This signal handler catches the signal TERMINATE (Ctrl+C) and calls the given function.

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