学习计划

**
1,学习目标

1:学会相关python测试框架

2:学会web自动化以及UI自动化相关知识

2,学习内容

1:python多线程处理,pip依赖管理与虚拟环境, python unittest测试框架, python unittest测试框架, 参数化用例,数据驱动, 测试报告美化与定制

2:python pytest测试实战

3,学习时间

提示:可以添加计划学习的时间
例如: 周一至周五晚上8点—晚上9点
周六上午 9 点-上午 11 点
周日下午 3 点-下午 6 点

4,学习产出

每次学习记录一篇博客

学习日记

什么是进程

1:进行是执行中的程序,拥有独立地址空间,内存,数据栈等,操作系统管理,派生新进程,进程间通信(IPC)方式共享信息,同线程下执行,并且共享相同的上下文,线程间的信息共享和同学更加简单,多线程并发执行,需要同步原语

python和线程

解释器主循环,主循环中只有一个控制线程在执行,使用的是全局解释器锁(GIL)

GIL如何保证一个线程

设置GIL,切换金一个线程去运行,

执行以下操作:指定数量的字节码执行,线程主动让出控制权,

爆显存设置会睡眠状态,解说GIL。重复上述步骤

两种线程管理:

_thread :提供了基本的线程和锁

threading:提供了更高级别,功能更全的线程管理,支持同步机制,支持守护线程

代码实操:

import logging
from time import sleep, ctime

logging.basicConfig(level=logging.INFO)

def loop0():
    logging.info("start loop at" + ctime())
    sleep(4)
    logging.info("end loop at" + ctime())



def loop1():
    logging.info("start loop at" + ctime())
    sleep(2)
    logging.info("end loop at" + ctime())


def main():
    logging.info("start loop at" + ctime())
    loop0()
    loop1()
    logging.info("end loop at" + ctime())
if __name__ == '__main':
    main()

代码优化1:

import logging
from time import sleep, ctime

logging.basicConfig(level=logging.INFO)

def loop0():
    logging.info("start loop at" + ctime())
    sleep(4)
    logging.info("end loop at" + ctime())



def loop1():
    logging.info("start loop at" + ctime())
    sleep(2)
    logging.info("end loop at" + ctime())


def main():
    logging.info("start loop at" + ctime())
    _thread.start_new_thread(loop0,())
    _thread.start_new_thread(loop1,())
    sleep(6)#为了线程可以更好的运行
    logging.info("end loop at" + ctime())
if __name__ == '__main':
    main()

代码优化2:

import logging
from time import sleep, ctime

logging.basicConfig(level=logging.INFO)


loop=[2, 4]
def loop(nloop, nesc, lock):#新增线程锁:lock
    logging.info("start loop" + shr(nloop) + " at" + ctime())
    sleep(nesc)
    logging.info("end loop" + shr(nloop) + " at" + ctime())
    lock.release()

def main():
    logging.info("start loop at" + ctime())
    lock = []
    nloops = range(len(loops))
    for i in nloops:
        lock = _thread.allocate_lock()
        lock.acquire
        lock.append(lock)
    for i in nloops:#将线程打开
        _thread.start_new_thread (loop, (i, loop[i], lock[1]))
    for i in nloops:#判断线程是否关闭
        while locks[i].locked:pass
    logging.info("end loop at" + ctime())
if __name__ == '__main':
    main()

替代代码锁的方法

import thread
import logging
import threading
from time import sleep, ctime

logging.basicConfig(level=logging.INFO)


loop=[2, 4]
def loop(nloop, nesc):#新增线程锁:lock
    logging.info("start loop" + shr(nloop) + " at" + ctime())
    sleep(nesc)
    logging.info("end loop" + shr(nloop) + " at" + ctime())

def main():
    logging.info("start loop at" + ctime())
    thread = []
    nloops = range(len(loops))
    for i in nloops:#将线程打开
        t = threading.thread (target=loop, args=(i, loop[i]))
        thread.append(t)
    for i in nloops
        threads[i].start()
    for i in nloops:#判断线程如果允许的话 就join到这里
        threads[i].join()
    logging.info("end loop at" + ctime())
if __name__ == '__main':
    main()
import thread
import logging
import threading
from time import sleep, ctime

logging.basicConfig(level=logging.INFO)


loop=[2, 4]
class MyThread(threading.Thread)#MyThread方法的构造
    def__init__(self, func, args, name='')
        threading.Thread.__init__(self)
        self.func = func
        self.args = args
        self.name = name

    def run(self):
        self.func(*self.args)
   
def loop(nloop, nesc):#新增线程锁:lock
    logging.info("start loop" + shr(nloop) + " at" + ctime())
    sleep(nesc)
    logging.info("end loop" + shr(nloop) + " at" + ctime())

def main():
    logging.info("start loop at" + ctime())
    thread = []
    nloops = range(len(loops))
    for i in nloops:#将线程打开
        t = threading.thread (target=loop, (i, loop[i], loop.__name__))
        thread.append(t)
    for i in nloops
        threads[i].start()
    for i in nloops:#判断线程如果允许的话 就join到这里
        threads[i].join()
    logging.info("end loop at" + ctime())
if __name__ == '__main':
    main()

原语,锁,信号量