python多线程结束线程_线程类及其对象-Python多线程

python多线程结束线程

In the threading module the most popular and the most used call is the Thread class, which is primarily used to create and run threads. Thread class provides all the major functionalities required to create and manage a thread.

threading模块中,最受欢迎和最常用的调用是Thread类,该类主要用于创建和运行线程。 Thread类提供了创建和管理线程所需的所有主要功能。

Thread objects are the objects of the Thread class where each object represents an activity to be performed in a separate thread of control.

线程对象是Thread类的对象,其中每个对象代表将在单独的控制线程中执行的活动。

There are two ways to create the Thread object and specify the activity to be performed:

有两种方法可以创建Thread对象并指定要执行的活动:

  • by passing a callable object to the constructor

    通过将可调用对象传递给构造函数

  • or, by overriding the run() method in a subclass.

    或者,通过重写子类中的run()方法。

Thread object which is created using constructor or run method can be started by using start() method. Whenever a Thread object starts a new thread then internally it's run() method is invoked.

使用构造函数或run方法创建的线程对象可以通过使用start()方法来start() 。 每当Thread对象启动新线程时,内部都会调用它的run()方法。

Here is a simple example:

这是一个简单的示例:

演示地址

In the example above, we have also used the time module to make one of the thread sleep.

在上面的示例中,我们还使用了time模块来使线程之一进入睡眠状态。

Basic syntax of the Thread class constructor is:

Thread类构造函数的基本语法为:

Thread(group=None, target=None, name=None, args=(), kwargs={})

We will explain the arguments of the Thread class constructor in the section below.

我们将在下面的部分中解释Thread类构造函数的参数。

线程如何工作? (How Thread works?)

Once we initialize a thread using the Thread class constructor, we must call its start() method to start the thread.

一旦使用Thread类构造函数初始化了线程,就必须调用其start()方法来启动线程。

When the thread starts, the thread is considered alive and active. When its run() method terminates, either normally, or due to an unhandled exception then the thread stops being alive or active. The isAlive() method tests whether the thread is alive or not at any given point of time.

当线程启动时,该线程被认为是活动活动的 。 当其run()方法正常终止或由于未处理的异常而终止时,线程将停止处于活动状态或活动状态。 isAlive()方法测试线程在任何给定时间点是否处于活动状态。

Other threads can call a thread's join() method to join any thread. This blocks the calling thread until the thread whose join() method is called is terminated.

其他线程可以调用线程的join()方法来加入任何线程。 这将阻塞调用线程,直到终止其join()方法的线程为止。

For example, in the code above, from the main thread, we call t1.join() and t2.join(), hence the main thread will wait for the threads t1 and t2 to terminate and then end.

例如,在上面的代码中,从线程调用t1.join()t2.join() ,因此线程将等待线程t1t2终止然后结束。

Every thread has a name associated with it. The name can be passed to the constructor, or we can set or retrieve name by using setname() and getname() methods respectively.

每个线程都有一个与之关联的名称。 可以将名称传递给构造函数,也可以分别使用setname()getname()方法设置或检索名称。

A flag daemon thread can be associated with any thread. The significance of this flag is that the entire python program exits when only daemon threads are left. The flag can be set or retrieved by using setDaemon() method and getDaemon() method respectively.

标志守护程序线程可以与任何线程关联。 该标志的重要性在于,仅保留守护程序线程时,整个python程序都会退出。 可以分别使用setDaemon()方法和getDaemon()方法设置或检索标志。

The main thread object corresponds to the initial thread of control in the python program. It is not a daemon thread.

主线程对象对应于python程序中的初始控制线程。 它不是守护程序线程。

Thread类中的函数和构造函数 (Functions and Constructor in the Thread class)

Now that we have seen a basic threading program with threads running, it's time to understand the code along with exploring all the important methods provided by the Thread class.

现在,我们已经看到了运行线程的基本线程程序,是时候了解代码并探索Thread类提供的所有重要方法了。

Thread类构造函数 (Thread class Constructor)

Following is the basic syntax of the Thread class constructor:

以下是Thread类构造函数的基本语法:

Thread(group=None, target=None, name=None, args=(), kwargs={})

The constructor allows many arguments, some of which are required while some are not. Let's see what they are:

构造函数允许使用许多参数,其中一些参数是必需的,而某些参数则不是必需的。 让我们看看它们是什么:

  • group: Should be None. It is reserved for future extension.

    group :应该为None 。 保留以供将来扩展。

  • target: This is the callable object or task to be invoked by the run() method. As you can see in the code example at the top, we have specified the function names thread1 and thread2 as the value for this argument. It's default value is None.

    target :这是run()方法要调用的可调用对象或任务。 如您在顶部的代码示例中所看到的,我们已指定函数名称thread1thread2作为此参数的值。 默认值为None

  • name: This is used to specify the thread name. By default, a unique name is generated following the format Thread-N, where N is a small decimal number.

    name :用于指定线程名称。 默认情况下,将按照Thread-N格式生成唯一名称,其中N是一个小十进制数字。

  • args: This is the argument tuple for the target invocation. We can provide values in it which can be used in the traget method. It's default value is empty, i.e. ()

    args :这是目标调用的参数元组 。 我们可以在其中提供可以在traget方法中使用的值。 它的默认值为空,即()

  • kwargs: This is keyword argument dictionary for the target invocation. This defaults to {}.

    kwargs :这是目标调用的关键字参数字典 。 默认为{}。

start()方法 (start() method)

This method is used to start the thread's activity. When we call this method, internally the run() method is invoked which executes the target function or the callable object.

此方法用于启动线程的活动。 当我们调用此方法时,将在内部调用run()方法,该方法执行目标函数或可调用对象。

run()方法 (run() method)

Method representing the thread's activity.

表示线程活动的方法。

You may override this method in a subclass extending the Thread class of the threading module. The standard run() method invokes the callable object passed to the object's constructor as the target argument with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

您可以在扩展线程模块的Thread类的子类中重写此方法。 标准的run()方法调用传递给对象构造函数的可调用对象作为目标参数,并分别从argskwargs参数中获取顺序和关键字参数。

Here we have a simple example, where we have created a subclass in which we will override the run() method.

在这里,我们有一个简单的示例,其中创建了一个子类,在该子类中将覆盖run()方法。

演示地址

join([timeout])方法 (join([timeout]) method)

When we call this method for any thread, it blocks the calling thread until the thread whose join() method is called terminates, either normally or through an unhandled exception.

当我们对任何线程调用此方法时,它都会阻塞调用线程,直到被调用join()方法的线程正常或通过未处理的异常终止为止。

If you want to provide the timeout argument, it should be a floating point number.

如果要提供timeout参数,则应为浮点数。

getName()方法 (getName() method)

This method is used to return the thread's name.

此方法用于返回线程的名称。

setName(name)方法 (setName(name) method)

Used for setting the thread's name. The name is a string used for identification purposes only.

用于设置线程的名称。 名称是仅用于标识目的的字符串。

isAlive()方法 (isAlive() method)

This method returns whether the thread is alive or not. A thread is alive from the moment the start() method returns until its run() method terminates.

此方法返回线程是否处于活动状态。 从start()方法返回到其run()方法终止的那一刻起,线程一直处于活动状态。

isDaemon()方法 (isDaemon() method)

This method is used to get the value of the thread's daemon flag.

此方法用于获取线程的守护程序标志的值。

setDaemon(daemonic)方法 (setDaemon(daemonic) method)

This method is used to set the thread's daemon flag to the Boolean value daemonic. This must be called before start() method is called.

此方法用于将线程的守护程序标志设置为布尔值daemonic 。 必须在调用start()方法之前调用它。

The entire Python program exits when no active non-daemon threads are left.

当没有活动的非守护线程时,整个Python程序将退出。

翻译自: https://www.studytonight.com/python/python-thread-object

python多线程结束线程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值