SoFunction
Updated on 2024-11-17

How are Python's sub-threads and sub-processes manually terminated?

How to End Python Subthreads and Subprocesses

Ways to end a subthread:.

This is to carry the code of other gods, I do not know the principle, anyway, fetishism, for the time being did not find any shortcomings, first use it.

import inspect
import ctypes
import threading
from time import sleep
 
 
def serial_read():
 
    while True:
 
        print("Chun is pure man!")
 
        sleep(1)
 
 
 
def _async_raise(tid, exctype):
    """raises the exception, performs cleanup if needed"""
    tid = ctypes.c_long(tid)
    if not (exctype):
        exctype = type(exctype)
    res = .PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
    if res == 0:
        raise ValueError("invalid thread id")
    elif res != 1:
        # """if it returns a number greater than one, you're in trouble,
        # and you should call it again with exc=NULL to revert the effect"""
        .PyThreadState_SetAsyncExc(tid, None)
        raise SystemError("PyThreadState_SetAsyncExc failed")
 
 
def stop_thread(thread):
    _async_raise(, SystemExit)
 
 
def Air():
 
    ords=0
 
    myThread = (target=serial_read)
 
    ()
 
    while True:
 
 
        ords+=1
 
        if ords==10:
 
            stop_thread(myThread)
            print("Stop the sub-thread.")
            break
 
 
        sleep(1)
 
 
if __name__ == '__main__':
 
    Air()

Here's how to end a child process:

import inspect
import ctypes
from time import sleep
from multiprocessing import Process
 
 
def serial_read():
 
    while True:
 
        print("Chun is pure man!")
 
        sleep(1)
 
 
def Air():
 
    ords=0
 
    myThread = Process(target=serial_read)
 
    ()
 
    while True:
 
 
        ords+=1
 
        if ords==10:
 
            ()
            print("Stop the subprocess")
            break
 
 
        sleep(1)
 
 
if __name__ == '__main__':
 
    Air()

If you want to end a child process or a child thread, you need to get the process object or thread object, but there is no way to realize the way to create a class attribute in a class and then use self.xxxxx to call it in other class methods, then create a class attribute list, and then assign the object to the list after you create a child process or a child thread, and then call the elements of the list in other methods of the class. Then call the elements of this list in other methods of the class, and you have the object of the subprocess or subthread.

Example:

def startss(self,a1,b1,c1,under,rough,blue,among):
 
        # Create new threads
        p1=(target=self.line01,args=(a1,b1,under,rough,)) #The sign must be added.
        p2=(target=self.line02,args=(a1,c1,under,blue,))
        p3=Process(target=self.Process01,args=(under,rough,blue,)) # Computing process
 
        #among is a container for the class attribute list
 
        (p1)
        (p2)
        (p3)
 
        # Start a new thread
        ()
        ()
 
        # Open the computational process
        ()

References:https:///article/

How to terminate or restart a child process in python multiprocessing

To this point this article on Python's sub-threads and sub-processes is how to manually end? The article is introduced to this, more related to Python's sub-threads and sub-processes content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!