python多进程多线程
Posted chenjie0818
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python多进程多线程相关的知识,希望对你有一定的参考价值。
一:多线程和多进程
进程是多个资源的集合。
线程是就是进程里面具体干活的。
线程和线程之间是互相独立的。
二:多线程使用threading模块
启用多线程:
import threading def down_load(): time.sleep(5) print("运行完了") t = threading.Thread(target=down_load,args=(‘name‘,‘abfd‘)) #生成一个线程实例 t.start 启动线程 复制代码
线程等待:
import threading import time def down_load(): time.sleep(5) print("运行完了") start_time = time.time() for i in range(5): t = threading.Thread(target=down_load) t.start() while threading.activeCount()!=1: pass print(threading.activeCount()) #查看当前线程数 print(threading.current_thread())#查看当前线程 end_time = time.time() print(end_time - start_time) 复制代码
下载图片:
import requests,time,threading from hashlib import md5 result_list = {} def down_load_pic(url): #下载图片的函数 req = requests.get(url) m = md5(url.encode()) file_name = m.hexdigest()+‘.png‘ with open(file_name ,‘wb‘) as fw: fw.write(req.content) result_list[file_name] = threading.current_thread() url_list = [‘http://www.nnzhp.cn/wp-content/uploads/2019/10/f410afea8b23fa401505a1449a41a133.png‘, ‘http://www.nnzhp.cn/wp-content/uploads/2019/11/481b5135e75c764b32b224c5650a8df5.png‘, ‘http://www.nnzhp.cn/wp-content/uploads/2019/11/b23755cdea210cfec903333c5cce6895.png‘, ‘http://www.nnzhp.cn/wp-content/uploads/2019/11/542824dde1dbd29ec61ad5ea867ef245.png‘] start_time = time.time() for url in url_list: t = threading.Thread(target=down_load_pic,args=(url,)) t.start() while threading.activeCount()!=1: pass end_time = time.time() print(end_time - start_time) print(result_list) 复制代码 异步任务,发送邮件: 复制代码 import yagmail,threading def send_mail(): #发送邮件函数 smtp = yagmail.SMTP(host=‘smtp.163.com‘, user=‘xxxxx@163.com‘, password=‘xxxxxx‘ ) smtp.send(to=‘niuhanyang@163.com‘,cc=[‘xxxxxx@163.com‘,‘xxxxxx@qq.com‘],subject=‘标题‘, contents=‘正文‘,attachments=[r‘/Users/xxx.py‘] ) def async_send_mail(): #通过线程发送邮件 t = threading.Thread(target=send_mail) t.start()
线程池,导入threadpool:
import threadpool import requests,time,threading from hashlib import md5 def down_load_pic(url): #下载图片函数 print(threading.current_thread()) req = requests.get(url) m = md5(url.encode()) with open( m.hexdigest()+‘.png‘,‘wb‘) as fw: fw.write(req.content) url_list = [‘http://www.nnzhp.cn/wp-content/uploads/2019/10/f410afea8b23fa401505a1449a41a133.png‘, ‘http://www.nnzhp.cn/wp-content/uploads/2019/11/481b5135e75c764b32b224c5650a8df5.png‘, ‘http://www.nnzhp.cn/wp-content/uploads/2019/11/b23755cdea210cfec903333c5cce6895.png‘, ‘http://www.nnzhp.cn/wp-content/uploads/2019/11/542824dde1dbd29ec61ad5ea867ef245.png‘] pool = threadpool.ThreadPool(20) #实例化一个线程池 reqs = threadpool.makeRequests(down_load_pic,url_list) #分配数据 [pool.putRequest(req) for req in reqs] #列表生成式 print(threading.activeCount()) pool.wait() #等待 print(‘end‘)
守护线程:
#主线程结束,守护线程立马死掉。比如浏览器下打开多个窗口,一旦关闭浏览器,下面所有窗口全部关闭 import threading,time def down_load(): time.sleep(5) print("运行完了") for i in range(10): t = threading.Thread(target=down_load) t.setDaemon(True) #设置子线程为守护线程 t.start() print(‘over‘)
线程锁:
复制代码 #多个线程操作同一个数据的时候,就得加锁 import threading num = 0 lock = threading.Lock() #申请一把锁 def add(): global num # lock.acquire()#加锁 # num+=1 # lock.release()#解锁 #死锁 with lock:#简写,用with也会帮你加锁,解锁 num+=1 for i in range(20): t = threading.Thread(target=add,) t.start() while threading.activeCount() !=1: pass print(num)
多进程,导入multiprocessing模块:
import multiprocessing,time def down_load(): time.sleep(5) print("运行完了") if __name__ == ‘__main__‘: for i in range(5): p = multiprocessing.Process(target=down_load) p.start() while len(multiprocessing.active_children())!=0:#等待子进程结束 pass print(multiprocessing.current_process()) print(‘end‘)
以上是关于python多进程多线程的主要内容,如果未能解决你的问题,请参考以下文章