美文网首页
python多线程,自我理解

python多线程,自我理解

作者: new和光同尘 | 来源:发表于2020-11-16 18:56 被阅读0次

前段时间写了自动化程序用到了多线程,当时稀里糊涂的从网上复制粘贴完成的需求,搞完之后,梳理了一下自己的理解,没有晦涩难懂的专业术语,也不说原理,就说怎么用,举例实操,直接切入!

其中两个用到多线程的点

  • 同时给20台终端刷机的自动化程序
  • 同时给多台终端安装三方应用

我创建线程使用的threading这个库

1. 普通的线程创建

import threading
import time
number = 0
def method(flag):
    global number
    for loop in range(3):
        number += 1
        print(flag + ' ' + str(number))
        time.sleep(1)

if __name__ == '__main__':
    threadList = []
    # 创建线程时,target=  调用的函数是不加括号的,
    # 不加括号,调用的是函数本身,是整个函数,是函数的一个对象,不用等到函数执行结束,不阻塞
    # 加括号,  调用的是函数的执行过程,必须等到函数执行结束,是阻塞主线程的
    t1 = threading.Thread(target=method, args=('t1',))
    t2 = threading.Thread(target=method, args=('t2',))
    threadList.append(t1)
    threadList.append(t2)
    for i in threadList:
        i.start()
    print('和光同尘')

这种情况的调用的流程图大概是这样的(根据自己的理解):


2. setDaemon(True) 守护线程

主进程,成为子进程的守护进程,意思就是主进程退出后,不管子进程是否结束,整个程序直接退出,setDeamon(True),在start()之前调用才会生效
比如说,拿手机举例,整个测试过程中要求设备亮屏测试,长时间不操作设备会自动息屏,这个时候我就可以启动一个守护线程判断手机屏幕的状态,只要灭屏,就点亮屏幕,主程序结束后,直接就退出

import threading
import time
number = 0
def method(flag):
    global number
    for loop in range(3):
        number += 1
        print(flag + ' ' + str(number))
        time.sleep(1)

if __name__ == '__main__':
    threadList = []
    t1 = threading.Thread(target=method, args=('t1',))
    t2 = threading.Thread(target=method, args=('t2',))
    threadList.append(t1)
    threadList.append(t2)
    for i in threadList:
        i.setDaemon(True)
        i.start()
    print('和光同尘')

3. join() 子进程执行结束之后,主进程再继续执行

直接举例,还是拿手机为例,比如:
给10台手机刷机/安装应用,10台手机全部刷完机/安装完应用 之后才能开始测试,创建了10个线程给 手机刷机/安装应用,就需要到join()

import threading
import time
number = 0
def method(flag):
    global number
    for loop in range(3):
        number += 1
        print(flag + ' ' + str(number))
        time.sleep(1)

if __name__ == '__main__':
    threadList = []
    t1 = threading.Thread(target=method, args=('t1',))
    t2 = threading.Thread(target=method, args=('t2',))
    threadList.append(t1)
    threadList.append(t2)
    for i in threadList:
        i.setDaemon(True)
        i.start()
    for j in threadList:
        j.join()
    print('和光同尘')

4. 线程锁Lock

直接上例子,比如厕所只有一个蹲坑,同时有一群人在排队,不管多着急,一次只允许进去一个,这个时候就用上线程锁 threading.Lock()
线程锁的方法
acquire()是锁定,类似上述例子中 蹲坑已经被占用,别人用不了了,
release()是释放锁,类似上述例子中蹲坑已经用完,开门出去,别人可以用了

import threading
import time
number = 0
def method(flag, mlock):
    global number
    mlock.acquire()
    for loop in range(3):
        number += 1
        print(flag + ' ' + str(number))
        time.sleep(1)
    mlock.release()

if __name__ == '__main__':
    threadList = []
    lock = threading.Lock()
    t1 = threading.Thread(target=method, args=('t1', lock,))
    t2 = threading.Thread(target=method, args=('t2', lock,))
    threadList.append(t1)
    threadList.append(t2)
    for i in threadList:
        i.setDaemon(True)
        i.start()
    for j in threadList:
        j.join()
    print('和光同尘')

5. 允许给定数量的子线程同时做操作BoundedSemaphore(),

用法同Lock也有两个操作方法 acquire() 和 release()
还是上例子,比如这次有3个蹲坑,或者饭店有3个餐桌 ..... 嘿嘿嘿 后续的应该可以脑补出来

import threading
import time
number = 0
def method(flag, mlock):
    global number
    mlock.acquire()
    for loop in range(2):
        number += 1
        print(flag + ' ' + str(number))
        time.sleep(1)
    mlock.release()

if __name__ == '__main__':
    threadList = []
    bound = threading.BoundedSemaphore(3)
    t1 = threading.Thread(target=method, args=('t1', bound,))
    t2 = threading.Thread(target=method, args=('t2', bound,))
    t3 = threading.Thread(target=method, args=('t3', bound,))
    t4 = threading.Thread(target=method, args=('t4', bound,))
    threadList.append(t1)
    threadList.append(t2)
    threadList.append(t3)
    threadList.append(t4)
    for i in threadList:
        i.setDaemon(True)
        i.start()
    for j in threadList:
        j.join()
    print('和光同尘')

#######################结束######################

相关文章

  • python多线程,自我理解

    前段时间写了自动化程序用到了多线程,当时稀里糊涂的从网上复制粘贴完成的需求,搞完之后,梳理了一下自己的理解,没有晦...

  • Python “多线程”及其适用范围和缺点

    Python多线程的一些理解: 1.多线程采用的是分时复用技术,即不存在真正的多线程,cpu做的事是快速地切换线程...

  • 理解 python 中多线程

    虽然python中由于GIL的机制致使多线程不能利用机器多核的特性,但是多线程对于我们理解并发模型以及底层操作非常...

  • python之单线程、多线程操作

    前言 单线程和多线程是什么自行google补脑 对于python 单线程和多线程的理解,我花了很长时间,搜索的大部...

  • Python多线程例子

    Python中一个多线程的例子写这个例子是为了加强记忆,方便理解

  • python中的多线程与并发

    引子:经常听别人说起并发这个概念,在python语言中怎么理解多线程,多线程和并发是什么关系,什么时候更适合选择多...

  • 2018-08-04(15.5)多线程

    python基础语法(15.5) 多线程 初步了解多线程 (个人理解)电脑的cpu一般情况下一次只能给一个进程提供...

  • GIL

    谈谈python的GIL、多线程、多进程 最近在看 Python 的多线程,经常我们会听到老手说:“python下...

  • 快乐的 asyncio 学习

    其实 asyncio 的学习一点也不快乐 一、python 的多线程和多进程 要想理解 asyncio 的异步编程...

  • Python多线程编程——多线程编程中的加锁机制

    如果大家对Python中的多线程编程不是很了解,推荐大家阅读之前的两篇文章:Python多线程编程——多线程基础介...

网友评论

      本文标题:python多线程,自我理解

      本文链接:https://www.haomeiwen.com/subject/bdrzbktx.html