The module I'm using loads C library via ctypes.LoadLibrary in __init__ and calls a function which creates two processes using pthread_create C api. Thread IDs are not stored anywhere. These processes contain while(1) loops that read and write to serial port. I want to be able to kill the library threads, use said serial port for other purposes and then restore the module functionality with importlib.reload. Right now serial port remains inaccessible until I kill python top-most script with ctrl+c.
Since you are not saving a reference to the thread. You can't kill it specifically. The easiest and most reliable way is to save the thread ID somewhere.
Some comments talk about listing threads to kill them. Don't do that! You can't tell which thread are your own and killing someone else thread is not nice. Just save the ID, it's simple and robust.
How to kill C pthreads created by python ctypes.LoadLibrary