Table of Contents
Multi-threading
Daemon thread:
Throwing exceptions in Python threads
signal.pthread_kill operation:
Multiprocessing
Multiple coroutines
Home Backend Development Python Tutorial What is the method to forcefully close threads, coroutines and processes in Python?

What is the method to forcefully close threads, coroutines and processes in Python?

Apr 20, 2023 pm 02:34 PM
python

Multi-threading

First of all, when exiting in a thread, we often use a method: the loop condition of the sub-thread execution sets a condition, and when we need to exit the sub-thread, set the condition , at this time the sub-thread will actively exit, but when the sub-thread is blocked, the conditions are not judged in the loop, and the blocking time is uncertain, it will be impossible for us to recycle the thread. At this time, the following methods are needed:

Daemon thread:

If you set a thread as a daemon thread, it means that you are saying that this thread is not important and will not be used in the process. When exiting, there is no need to wait for this thread to exit.
If your main thread does not need to wait for those child threads to complete when it exits, then set the daemon attributes of these threads. That is, before the thread starts (thread.start()), call the setDeamon() function to set the daemon flag of the thread. (thread.setDaemon(True)) means that this thread is "not important".

If you want to wait for the child thread to complete before exiting, then there is nothing to do. , or explicitly call thread.setDaemon(False) to set the daemon value to false. The new child thread will inherit the daemon flag of the parent thread. The entire Python will end after all non-daemon threads exit, that is, when there are no non-daemon threads in the process.

That is, the sub-thread is a non-deamon thread, and the main thread does not exit immediately

import threading
import time
import gc
import datetime

def circle():
    print("begin")
    try:
        while True:
            current_time = datetime.datetime.now()
            print(str(current_time) + ' circle.................')
            time.sleep(3)
    except Exception as e:
        print('error:',e)
    finally:
        print('end')


if __name__ == "__main__":
   t = threading.Thread(target=circle)
   t.setDaemon(True)
   t.start()
   time.sleep(1)
   # stop_thread(t)
   # print('stoped threading Thread') 
   current_time = datetime.datetime.now()
   print(str(current_time) + ' stoped after') 
   gc.collect()
   while True:
      time.sleep(1)
      current_time = datetime.datetime.now()
      print(str(current_time) + ' end circle')
Copy after login

Is it the main thread that controls it?

The daemon thread needs the main thread to exit to complete the sub-thread exit, the following is the code, and then encapsulate one layer to verify whether the main thread needs to exit

def Daemon_thread():
   circle_thread= threading.Thread(target=circle)
#    circle_thread.daemon = True
   circle_thread.setDaemon(True)
   circle_thread.start()   
   while running:
        print('running:',running) 
        time.sleep(1)
   print('end..........') 


if __name__ == "__main__":
    t = threading.Thread(target=Daemon_thread)
    t.start()   
    time.sleep(3)
    running = False
    print('stop running:',running) 
    print('stoped 3') 
    gc.collect()
    while True:
        time.sleep(3)
        print('stoped circle')
Copy after login

Replace the main function execution , and found that the circle thread continued to execute after printing the stopped 3 flag.

Conclusion: The main thread relies on processing signals. Only by ensuring that it is alive can the signal be processed correctly.

Throwing exceptions in Python threads

Although using PyThreadState_SetAsyncExc can satisfy our direct exit from the thread in most cases; the PyThreadState_SetAsyncExc method only executes the "plan" for the thread to exit. It does not kill the thread, especially when it is executing an external C library. Try sleep(100) your method to kill one. It will be "killed" after 100 seconds. while flag: It works the same as ->flag = False method.

So when the sub-thread has blocking functions such as sleep, during the sleep process, the sub-thread cannot respond and will be captured by the main thread, resulting in the inability to cancel the sub-thread. In fact, when the thread is sleeping, it is not possible to directly use the async_raise function to kill the thread, because if the thread is busy outside the Python interpreter, it will not catch the interrupt

Sample code:

import ctypes
import inspect
import threading
import time
import gc
import datetime

def async_raise(tid, exctype):
   """raises the exception, performs cleanup if needed"""
   tid = ctypes.c_long(tid)
   if not inspect.isclass(exctype):
      exctype = type(exctype)
   res = ctypes.pythonapi.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"""  
      ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
      raise SystemError("PyThreadState_SetAsyncExc failed")
      
def stop_thread(thread):
   async_raise(thread.ident, SystemExit)
   
def circle():
    print("begin")
    try:
        while True:
            current_time = datetime.datetime.now()
            print(str(current_time) + ' circle.................')
            time.sleep(3)
    except Exception as e:
        print('error:',e)
    finally:
        print('end')

if __name__ == "__main__":
   t = threading.Thread(target=circle)
   t.start()
   time.sleep(1)
   stop_thread(t)
   print('stoped threading Thread') 
   current_time = datetime.datetime.now()
   print(str(current_time) + ' stoped after') 
   gc.collect()
   while True:
      time.sleep(1)
      current_time = datetime.datetime.now()
      print(str(current_time) + ' end circle')
Copy after login

signal.pthread_kill operation:

This is the closest to the pthread kill operation in Unix. I saw some uses on the Internet, but I did not find the use in this library when I verified it.

This is From the description in python's official signal explanation document, I saw that it is a new function in version 3.3. I am python3.10 myself and there is no pthread_kill. It may be removed in subsequent versions.

What is the method to forcefully close threads, coroutines and processes in Python?

This is some sample code I saw online, but it cannot be executed. If anyone knows how to use it, you can communicate.

from signal import pthread_kill, SIGTSTP
from threading import Thread
from itertools import count
from time import sleep

def target():
    for num in count():
        print(num)
        sleep(1)

thread = Thread(target=target)
thread.start()
sleep(5)
signal.pthread_kill(thread.ident, SIGTSTP)
Copy after login

Multiprocessing

Multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package provides both local and remote concurrent operations, effectively bypassing the global interpreter lock by using child processes instead of threads. Therefore, the multiprocessing module allows programmers to take full advantage of multiple processors on a given machine.

Multiprocess libraries are used, and we can call its internal function terminate to help us release them. For example, t.terminate() can force the child process to exit.

However, the interaction method using multi-process data is more cumbersome. Shared memory, pipe or message queue must be used for data interaction between the child process and the parent process.

The sample code is as follows:

import time
import gc
import datetime
import multiprocessing

def circle():
    print("begin")
    try:
        while True:
            current_time = datetime.datetime.now()
            print(str(current_time) + ' circle.................')
            time.sleep(3)
    except Exception as e:
        print('error:',e)
    finally:
        print('end')


if __name__ == "__main__":
    t = multiprocessing.Process(target=circle, args=())
    t.start()
    # Terminate the process
    current_time = datetime.datetime.now()
    print(str(current_time) + ' stoped before') 
    time.sleep(1)
    t.terminate()  # sends a SIGTERM
    current_time = datetime.datetime.now()
    print(str(current_time) + ' stoped after') 
    gc.collect()
    while True:
        time.sleep(3)
        current_time = datetime.datetime.now()
        print(str(current_time) + ' end circle')
Copy after login

Multiple coroutines

Coroutines (coroutines) are also called micro-threads. They are another way to achieve multi-tasking and are more efficient than threads. A small execution unit, generally running on a single process and single thread. Because it has its own CPU context, it can switch tasks through a simple event loop, which is more efficient than switching between processes and threads, because the switching between processes and threads is performed by the operating system.

Python mainly relies on two libraries to implement coroutines: asyncio (asyncio is a standard library introduced from Python 3.4, which directly has built-in support for coroutine asynchronous IO. The essence of asyncio's programming model is a message Loop, we usually define a coroutine function (or task) first, obtain the event loop loop from the asyncio module, and then throw the coroutine task (or task list) that needs to be executed into the loop for execution, thus achieving asynchronous IO) and gevent (Gevent is a third-party library that can easily implement concurrent synchronous or asynchronous programming through gevent. The main mode used in gevent is Greenlet, which is a lightweight coroutine connected to Python in the form of a C extension module.).

由于asyncio已经成为python的标准库了无需pip安装即可使用,这意味着asyncio作为Python原生的协程实现方式会更加流行。本文仅会介绍asyncio模块的退出使用。

使用协程取消,有两个重要部分:第一,替换旧的休眠函数为多协程的休眠函数;第二取消使用cancel()函数。

其中cancel() 返回值为 True 表示 cancel 成功。

示例代码如下:创建一个coroutine,然后调用run_until_complete()来初始化并启动服务器来调用main函数,判断协程是否执行完成,因为设置的num协程是一个死循环,所以一直没有执行完,如果没有执行完直接使用 cancel()取消掉该协程,最后执行成功。

import asyncio
import time


async def num(n):
    try:
        i = 0
        while True:
            print(f'i={i} Hello')
            i=i+1
            # time.sleep(10)
            await asyncio.sleep(n*0.1)
        return n
    except asyncio.CancelledError:
        print(f"数字{n}被取消")
        raise


async def main():
    # tasks = [num(i) for i in range(10)]
    tasks = [num(10)]
    complete, pending = await asyncio.wait(tasks, timeout=0.5)
    for i in complete:
        print("当前数字",i.result())
    if pending:
        print("取消未完成的任务")
        for p in pending:
            p.cancel()


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(main())
    finally:
        loop.close()
Copy after login

The above is the detailed content of What is the method to forcefully close threads, coroutines and processes in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles