selenium自学之路:多线程(一)

概念:
什么是进程:进程是程序的一次执行,每个进程都有自己的地址空间、内存、数据栈,以及其他记录其运行轨迹的辅助数据。操作系统管理在其上面运行的所有进程,并为这些进程公平的分配时间
什么是线程:线程(有时被称为轻量级进程)与进程有些相似,不同的是,所有线程都运行在同一个进程中,共享相同的运行环境

单线程时代:
处理器需要处理多个任务时,必须对这些任务安排执行顺序,并按照这个顺序来执行任务
例如:

from time import sleep,ctime

def music():
    print('I was listening to music! %s'%ctime())
    sleep(2)

def movie():
    print('I was at the movies! %s'%ctime())
    sleep(5)

if __name__ == '__main__':
    music()
    movie()
    print('all end %s'%ctime())

运行结果:

I was listening to music! Mon Apr 22 11:10:03 2019
I was at the movies! Mon Apr 22 11:10:05 2019
all end Mon Apr 22 11:10:10 2019

现在修改一下,使其变得更有趣:

from time import sleep,ctime

def music(func,loop):
    for i in range(loop):
        print('I was listening to %s! %s'%(func,ctime()))
        sleep(2)

def movie(func,loop):
    for i in range(loop):
        print('I was at the %s! %s'%(func,ctime()))
        sleep(5)

if __name__ == '__main__':
    music('铃儿响叮当',2)
    movie('大头儿子小头爸爸',2)
    print('all end %s'%ctime())

运行结果:

I was listening to 铃儿响叮当! Mon Apr 22 11:13:28 2019
I was listening to 铃儿响叮当! Mon Apr 22 11:13:30 2019
I was at the 大头儿子小头爸爸! Mon Apr 22 11:13:32 2019
I was at the 大头儿子小头爸爸! Mon Apr 22 11:13:37 2019
all end Mon Apr 22 11:13:42 2019

多线程技术:python通过两个标准库thread和threading提供对线程的支持。thread提供了低级别的,原始的线程以及一个简单的锁。threading基于java的线程模型设计。锁(Lock)和条件变量(condition)在java中是对象的基本行为(每一个对象都自带了锁和条件变量),而在python中则是独立的对象

我们应避免使用thread模块,原因是它不支持守护线程。当主进程退出时,所有的子线程不管它们是否还在工作,都会被强行退出

from time import sleep,ctime
import threading
def music(func,loop):
    for i in range(loop):
        print('I was listening to %s! %s'%(func,ctime()))
        sleep(2)

def movie(func,loop):
    for i in range(loop):
        print('I was at the %s! %s'%(func,ctime()))
        sleep(5)

threads=[]
t1=threading.Thread(target=music,args=('铃儿响叮当',2))
threads.append(t1)
t2=threading.Thread(target=movie,args=('大头儿子小头爸爸',2))
threads.append(t2)

if __name__ == '__main__':
    for t in threads:
        t.start()
    for t in threads:
        t.join()          #join()方法的作用是调用线程等待该线程完成后,才能继续用下运行。
    print('all end %s'%ctime())

运行结果:

I was listening to 铃儿响叮当! Mon Apr 22 11:22:34 2019
I was at the 大头儿子小头爸爸! Mon Apr 22 11:22:34 2019
I was listening to 铃儿响叮当! Mon Apr 22 11:22:36 2019
I was at the 大头儿子小头爸爸! Mon Apr 22 11:22:39 2019
all end Mon Apr 22 11:22:44 2019

优化线程的创建
从上面例子中发现线程的创建是颇为麻烦的,每创建一个线程都需要创建一个t(t1,t2…),当创建的线程较多时会极其不便,下面对例子进行改进

from time import sleep,ctime
import threading

def super_player(file_,time):
    for i in range(2):
        print('Start playing: %s! %s'%(file_,ctime()))
        sleep(time)

lists={'铃儿响叮当.mp3':3,'大头儿子小头爸爸.mp4':5,'舒克和贝塔.mp4':4}
threads=[]
files=range(len(lists))

for file_ ,time in  lists.items():
    t=threading.Thread(target=super_player,args=(file_,time))
    threads.append(t)

if __name__ == '__main__':
    for t in files:
        threads[t].start()
    for t in files:
        threads[t].join()
    print('end: %s'%ctime())

运行结果:

Start playing: 铃儿响叮当.mp3! Mon Apr 22 15:22:56 2019
Start playing: 大头儿子小头爸爸.mp4! Mon Apr 22 15:22:56 2019
Start playing: 舒克和贝塔.mp4! Mon Apr 22 15:22:56 2019
Start playing: 铃儿响叮当.mp3! Mon Apr 22 15:22:59 2019
Start playing: 舒克和贝塔.mp4! Mon Apr 22 15:23:00 2019
Start playing: 大头儿子小头爸爸.mp4! Mon Apr 22 15:23:01 2019
end: Mon Apr 22 15:23:06 2019

创建线程类:

from time import sleep,ctime
import threading
#创建MyThread类,用于继承threading.Thread
class MyThread(threading.Thread):
    #参数初始化
    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 super_player(file_,time):
    for i in range(2):
        print('Start playing: %s! %s'%(file_,ctime()))
        sleep(time)

lists={'铃儿响叮当.mp3':3,'大头儿子小头爸爸.mp4':5,'舒克和贝塔.mp4':4}
threads=[]
files=range(len(lists))

for file_ ,time in  lists.items():
    t=MyThread(super_player,(file_,time),super_player.__name__)
    threads.append(t)

if __name__ == '__main__':
    for t in files:
        threads[t].start()
    for t in files:
        threads[t].join()
    print('end: %s'%ctime())

运行结果:

Start playing: 铃儿响叮当.mp3! Mon Apr 22 15:35:00 2019
Start playing: 大头儿子小头爸爸.mp4! Mon Apr 22 15:35:00 2019
Start playing: 舒克和贝塔.mp4! Mon Apr 22 15:35:00 2019
Start playing: 铃儿响叮当.mp3! Mon Apr 22 15:35:03 2019
Start playing: 舒克和贝塔.mp4! Mon Apr 22 15:35:04 2019
Start playing: 大头儿子小头爸爸.mp4! Mon Apr 22 15:35:05 2019
end: Mon Apr 22 15:35:10 2019

你可能感兴趣的:(selenium)