python多线程

1、线程:

程序的最小的执行单位
py文件就是一个进程,这个进程里面本来就是有一个线程的,本来的这个线程,就叫做主线程
线程和线程之间是独立的。

创建、启动线程
t = threading.Thread(target=create_data,args=(i,)) #创建线程
t.start() #启动线程、
threading.activeCount() #当前线程数
t.join() #等待线程
主线程等待子线程的两种方式
import threading
import time,random
def create_data(url):
    t = random.randint(1,5)
    print("t..",t)
    time.sleep(t)
    print("url",url)

start_time = time.time()

#第一种主线程等待子线程的方式
# threads = []
# for i in range(5):
#     t = threading.Thread(target=create_data,args=(i,))
#     t.start()
#     threads.append(t)
#
# for t in threads:
#     t.join()  #等待线程

#第二种 通过判断当前线程数的方式
for i in range(5):
    t = threading.Thread(target=create_data,args=(i,))
    t.start()

while threading.activeCount() !=1:
    pass

end_time = time.time()

print('执行时间',end_time - start_time)

print(threading.activeCount()) #当前的线程数

2、多线程-- 锁

 ·  多个线程同时操作同一个数据的时候

lock = threading.Lock()
lock.acquire() #创建锁
lock.release() #释放锁
创建锁后需要释放锁,不然会成为死锁
import threading
count = 0

lock = threading.Lock()

def add_data():
    print("当前的线程是",threading.currentThread())
    global count
    lock.acquire() #创建锁
    count+=1
    lock.release()#释放锁
    with lock:
        count+=1


for i in range(10):
    t = threading.Thread(target=add_data )
    t.start()

while threading.activeCount() != 1:
    pass

print(count)

3、守护线程:

主线程结束,子线程也全部结束
t.setDaemon(True) #把子线程设置成守护线程
import threading
import time
def ganhuo():
    time.sleep(10)
    print("done!")

for i in  range(10):
    t = threading.Thread(target=ganhuo)
    t.setDaemon(True) #把子线程设置成守护线程
    t.start()

print("主线程活干完了")
4、线程池:threadpool 模块
pool = threadpool.ThreadPool(10) #创建线程池,参数是线程数
import requests,threading
import os,time,random
import threadpool

image_url = "https://q4.qlogo.cn/g?b=qq&nk=%s&s=140"

# qq_numbers = ["511402865","260303838","1099276647","1490768397","729226162",
#       "1164019076","569380213","923296994","330547222","609924484","1054114694",
#       "854795345"]
qq_numbers = [
    ["511402865","http://511402865"],
    ["511402861","http://511402865"],
    ["511402861","http://511402865"],
    ["511402861","http://511402865"],
    ["511402861","http://511402865"],
    ["511402861","http://511402865"],
    ["511402861","http://511402865"],
    ["511402861","http://511402865"],
]
def down_load_pic(arg_list): #qq,url,
    qq = arg_list[0]
    url = arg_list[1]
    print(qq,url)
    # url = image_url % qq
    # req = requests.get(url)
    # path = os.path.join("images",qq+'.jpg')
    # with open(path,'wb') as fw:
    #     fw.write(req.content)

pool = threadpool.ThreadPool(10)

reqs = threadpool.makeRequests(down_load_pic,qq_numbers)#分配数据
for req in reqs: #把分配好的数据,放到线程池里面
    pool.putRequest(req)

pool.wait() #开始运行

5、继承的方式启动线程:

import threading,time

class MyThread(threading.Thread):
    def __init__(self,name):
        super().__init__()
        self.name = name


    def run(self):
        time.sleep(1)
        # print("%s创建数据"%self.name)
        print("创建数据")

for i in range(5):
    t = MyThread("name")
    t.start()

 



posted @ 2021-07-07 23:26  黑。白。  阅读(46)  评论(0编辑  收藏  举报