Python多线程以及多线程中join()的使用方法示例

作者:waws520 时间:2021-02-07 16:25:03 

Python多线程与多进程中join()方法的效果是相同的。

下面仅以多线程为例:

首先需要明确几个概念:

知识点一:

当一个进程启动之后,会默认产生一个主线程,因为线程是程序执行流的最小单元,当设置多线程时,主线程会创建多个子线程,在python中,默认情况下(其实就是setDaemon(False)),主线程执行完自己的任务以后,就退出了,此时子线程会继续执行自己的任务,直到自己的任务结束,

见下面 例子一。

知识点二:

当我们使用setDaemon(True)方法,设置子线程为守护线程时,主线程一旦执行结束,则全部线程全部被终止执行,可能出现的情况就是,子线程的任务还没有完全执行结束,就被迫停止,

见下面例子二。

知识点三:

此时join的作用就凸显出来了,join所完成的工作就是线程同步,即主线程任务在设置join函数的地方,进入阻塞状态,一直等待其他的子线程执行结束之后,主线程再开始执行直到终止终止,

例子见下面三。

知识点四:

join有一个timeout参数:

  • 当有设置守护线程时,含义是主线程对于子线程等待timeout的时间将会杀死该子线程,最后退出程序。所以说,如果有10个子线程,全部的等待时间就是每个timeout的累加和。简单的来说,就是给每个子线程一个timeout的时间,让他去执行,时间一到,不管任务有没有完成,直接杀死。

  • 没有设置守护线程时,主线程将会等待timeout的累加和这样的一段时间,时间一到,主线程结束,但是并没有杀死子线程,子线程依然可以继续执行,直到子线程全部结束,程序退出。

一:Python多线程的默认情况


import threading
import time

def run():
   time.sleep(2)
   print('当前线程的名字是: ', threading.current_thread().name)
   time.sleep(2)

if __name__ == '__main__':

start_time = time.time()

print('这是主线程:', threading.current_thread().name)
   thread_list = []
   for i in range(5):
       t = threading.Thread(target=run)
       thread_list.append(t)

for t in thread_list:
       t.start()

print('主线程结束!' , threading.current_thread().name)
   print('一共用时:', time.time()-start_time)

其执行结果如下:

Python多线程以及多线程中join()的使用方法示例

关键:

  • 计时是对主线程计时,主线程结束,计时随之结束,打印出主线程的用时。

  • 主线程的任务完成之后,主线程随之结束,子线程继续执行自己的任务,直到全部的子线程的任务全部结束,程序结束。

二:设置守护线程


import threading
import time

def run():

time.sleep(2)
   print('当前线程的名字是: ', threading.current_thread().name)
   time.sleep(2)

if __name__ == '__main__':

start_time = time.time()

print('这是主线程:', threading.current_thread().name)
   thread_list = []
   for i in range(5):
       t = threading.Thread(target=run)
       thread_list.append(t)

for t in thread_list:
       t.setDaemon(True)
       t.start()

print('主线程结束了!' , threading.current_thread().name)
   print('一共用时:', time.time()-start_time)

注意:注意请确保setDaemon()在start()之前

其执行结果如下:

Python多线程以及多线程中join()的使用方法示例

关键点:

非常明显的看到,主线程结束以后,子线程还没有来得及执行,整个程序就退出了。

三:join的作用


import threading
import time

def run():

time.sleep(2)
   print('当前线程的名字是: ', threading.current_thread().name)
   time.sleep(2)

if __name__ == '__main__':

start_time = time.time()

print('这是主线程:', threading.current_thread().name)
   thread_list = []
   for i in range(5):
       t = threading.Thread(target=run)
       thread_list.append(t)

for t in thread_list:
       t.setDaemon(True)
       t.start()

for t in thread_list:
       t.join()

print('主线程结束了!' , threading.current_thread().name)
   print('一共用时:', time.time()-start_time)

其执行结果如下:

Python多线程以及多线程中join()的使用方法示例

关键点:

可以看到,主线程一直等待全部的子线程结束之后,主线程自身才结束,程序退出。

主程序意外退出的情况

在线程A中使用B.join()表示线程A在调用join()处被阻塞,且要等待线程B的完成才能继续执行


import threading
import time

def child_thread1():
   for i in range(10):
       time.sleep(1)
       print('child_thread1_running...')

def child_thread2():
   for i in range(5):
       time.sleep(1)
       print('child_thread2_running...')

def parent_thread():
   print('parent_thread_running...')
   thread1 = threading.Thread(target=child_thread1)
   thread2 = threading.Thread(target=child_thread2)
   thread1.setDaemon(True)
   thread2.setDaemon(True)
   thread1.start()
   thread2.start()
   thread2.join()
   1/0
   thread1.join()
   print('parent_thread_exit...')

if __name__ == "__main__":
   parent_thread()

输出:

parent_thread_running...
child_thread1_running...
child_thread2_running...
child_thread1_running...
child_thread2_running...
child_thread1_running...
child_thread2_running...
child_thread1_running...
child_thread2_running...
child_thread1_running...
child_thread2_running...
Traceback (most recent call last):
  File "E:/test_thread.py", line 31, in <module>
    parent_thread()
  File "E:/test_thread.py", line 25, in parent_thread
    1/0
ZeroDivisionError: integer division or modulo by zero

主线程在执行到thread2.join()时被阻塞,等待thread2结束后才会执行下一句

1/0会使主线程报错退出,且thread1设置了daemon=True,因此主线程意外退出时thread1也会立即结束。thread1.join()没有被主线程执行

总结

来源:https://juejin.cn/post/6979834817841528863

标签:python,多线程,join()
0
投稿

猜你喜欢

  • Python二进制数据结构Struct的具体使用

    2022-07-10 00:01:59
  • Ubuntu下mysql安装和操作图文教程

    2024-01-25 15:53:30
  • JS如何生成一个不重复的ID的函数

    2024-04-17 09:48:51
  • .NET之生成数据库全流程实现

    2024-01-16 05:08:48
  • Python 运行 shell 获取输出结果的实例

    2023-08-02 16:51:18
  • vue实现购物车功能(商品分类)

    2023-07-02 16:34:30
  • 扫盲大讲堂:SQL查询结果集对注入的影响及利用

    2009-09-05 09:49:00
  • 快速修复损坏的MySQL数据库

    2024-01-20 07:38:42
  • Pandas GroupBy对象 索引与迭代方法

    2022-12-08 17:32:12
  • 使用MySQL实现select into临时表的功能

    2024-01-25 01:14:53
  • Vue Element使用icon图标教程详解(第三方)

    2023-07-02 16:28:43
  • Oracle数据库下载及安装图文操作步骤

    2024-01-26 11:15:49
  • 在asp中使用js的encodeURIComponent方法

    2012-11-30 20:05:53
  • dpn网络的pytorch实现方式

    2023-11-20 10:37:19
  • Git Submodule管理项目子模块的使用

    2023-10-06 20:34:57
  • 对python中基于tcp协议的通信(数据传输)实例讲解

    2023-04-18 14:36:10
  • 求任意自然数内的素数

    2009-10-15 12:21:00
  • python中_del_还原数据的方法

    2022-07-28 18:49:14
  • Node+OCR实现图像文字识别功能

    2024-04-22 13:01:41
  • Vue3新属性之css中使用v-bind的方法(v-bind in css)

    2024-05-28 16:01:07
  • asp之家 网络编程 m.aspxhome.com