赞
踩
python多线程结束线程
In this tutorial we will introduce you to the concept of Multithreading and how threads can be implemented in python programming language. So let's start with understanding what threads are.
在本教程中,我们将向您介绍“多线程”的概念以及如何用python编程语言实现线程。 因此,让我们从了解什么是线程开始。
Threads are lightweight processes (subparts of a large process) that can run concurrently in parallel to each other, where each thread can perform some task. Threads are usually contained in processes. More than one thread can exist within the same process. Within the same process, threads share memory and the state of the process.
线程是轻量级进程(大型进程的子部分),可以彼此并行并行运行,其中每个线程都可以执行某些任务。 线程通常包含在进程中。 同一进程中可以存在多个线程。 在同一进程中,线程共享内存和进程状态。
There are two kinds of thread:
有两种线程:
Kernel-level threads
内核级线程
User level threads
用户级线程
Below we have explained a few differences between the two:
下面我们解释了两者之间的一些区别:
Kernel-level threads | User level Thread |
---|---|
Recognized by the operating system. | Does not recognized by the operating system. |
Implemented by the operating system. | Implemented by a user of the system. |
Implementation is complex. | Implementation is simple and easy. |
Solaris is an example | Posix is an example |
Requires hardware support | Requires no hardware support |
内核级线程 | 用户级线程 |
---|---|
被操作系统识别。 | 操作系统无法识别。 |
由操作系统实现。 | 由系统的用户实施。 |
实现很复杂。 | 实现很简单。 |
Solaris就是一个例子 | Posix是一个例子 |
需要硬件支持 | 无需硬件支持 |
Now that we have a basic idea about what threads are, let's try to understand the concept of Multithreading.
现在我们对什么是线程有了基本的了解,让我们尝试了解多线程的概念。
Modern computers have a CPU that has multiple processing cores and each of these cores can run many threads simultaneously which gives us the ability to perform several tasks concurrently. This process of running multiple Threads concurrently to perform tasks parallely is called Multithreading.
现代计算机具有一个具有多个处理核心的CPU,每个核心可以同时运行多个线程,这使我们能够同时执行多个任务。 同时运行多个线程以并行执行任务的过程称为多线程 。
Multithreading provides the following benefits:
多线程具有以下优点:
Multiple threads within a process share the same data space and can, therefore, share information or communicate with each other more easily than if they were separate processes.
一个进程中的多个线程共享相同的数据空间,因此与单独的进程相比,可以更轻松地共享信息或彼此通信。
Threads do not require much memory overhead; they are cheaper than processes in terms of memory requirements.
线程不需要太多的内存开销。 就内存需求而言,它们比进程便宜。
Multithreaded programs can run faster on computer systems with multiple CPUs because these threads can be executed concurrently.
多线程程序可以在具有多个CPU的计算机系统上更快地运行,因为这些线程可以同时执行。
Let's say you create a simple application for an Event's registration where attendees must register if they wish to attend the event. You have a simple HTML form for the users to fill in, and a backend which is a single threaded application.
假设您为活动注册创建了一个简单的应用程序,如果与会者希望参加该活动,则必须注册。 您有一个简单HTML表单供用户填写,后端是一个单线程应用程序。
As the application is single threaded, it can only process one request at a time. But what if the event is a "Coldplay's Music Concert" where millions of people want to register. Processing one request at a time will drastically slow down the performance.
由于应用程序是单线程的,因此它一次只能处理一个请求。 但是,如果该活动是“ Coldplay音乐音乐会”,那么会有数百万人希望注册。 一次处理一个请求将大大降低性能。
So, we make the application multi-threaded and start multiple threads inside it hence allowing parallel processing.
因此,我们使应用程序成为多线程并在其中启动多个线程,从而允许并行处理。
For performing multithreading in Python threading
module is used.The threading
module provides several functions/methods to implement multithreading easily in python.
用于执行在Python多线程threading
模块是,低脂threading
模块提供若干功能/方法来实现在python容易多线程。
Before we start using the threading
module, we would like to first introduce you to a module named time
, which provides a time()
, ctime()
etc functions which we will be using very often to get the current system time and another crucial function sleep()
which is used to suspend the execution of the current thread for a given number of seconds.
在开始使用threading
模块之前,我们想先向您介绍一个名为time
的模块,该模块提供一个time()
, ctime()
等函数,我们将经常使用这些函数来获取当前系统时间以及另一个关键函数sleep()
,用于将当前线程的执行暂停给定的秒数。
For example,
例如,
Now let's see how we can use the threading
module to start multiple threads.
现在让我们看看如何使用threading
模块来启动多个线程。
Let's try to understand the above code:
让我们尝试理解上面的代码:
We imported the thread
class using import threading
statement and we also imported the time
module. To create a new thread, we create an object of te Thread
class. It takes the following arguments:
我们使用import threading
语句导入了thread
类,并且还导入了time
模块。 要创建一个新线程,我们创建一个Thread
类的对象。 它采用以下参数:
target
: The function which will be executed by the thread.
target
:线程将执行的功能。
args
: The arguments to be passed to the target function. We can pass multiple arguments separated by comma.
args
:要传递给目标函数的参数。 我们可以传递多个用逗号分隔的参数。
In the example above, we created 2 threads with different target functions i.e. thread1(i)
and thread2(i)
.
在上面的示例中,我们创建了2个具有不同目标函数的线程,即thread1(i)
和thread2(i)
。
To start a thread, we have used the start()
method of the Thread
class.
要启动线程,我们使用了Thread
类的start()
方法。
We have also used the time.sleep()
method of the time
module to pause the execution of thread1
for 3 seconds.
我们还使用了time
模块的thread1
time.sleep()
方法将time.sleep()
的执行暂停了3秒钟。
Once the threads start, the current program (you can think of it as the main thread) also keeps on executing. In order to prevent the main program from completing its execution until the thread execution is complete, we use the join()
method.
线程启动后,当前程序(您可以将其视为主线程)也继续执行。 为了防止主程序在线程执行完成之前完成其执行,我们使用join()
方法。
As a result, the current program will wait for the completion of t1
and t2
and only after their execution is finished, the remaining statements of the current program will get executed i.e the statement print('Execution completed.')
.
结果,当前程序将等待t1
和t2
的完成,并且只有在它们完成执行之后,当前程序的其余语句才会被执行,即语句print('Execution completed.')
。
You should try running the above code once after commenting out the code on line number 16,17 where we use the join
method and see the result.
将第16,17行的代码注释掉后,您应该尝试运行上述代码一次,在此我们使用join
方法并查看结果。
So with this our basic introduction to multithreading in python is completed and now we know how to create and start multiple threads in python.
因此,到此,我们对python中的多线程的基本介绍已完成,现在我们知道了如何在python中创建和启动多个线程。
翻译自: https://www.studytonight.com/python/multithreading-in-python
python多线程结束线程
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。