ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

python多进程、多线程

2019-11-20 15:58:39  阅读:171  来源: 互联网

标签:load python print threading 线程 time 进程 import 多线程


一:多线程和多进程

进程是多个资源的集合。

线程是就是进程里面具体干活的。

线程和线程之间是互相独立的。

二:多线程使用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')

 

 

标签:load,python,print,threading,线程,time,进程,import,多线程
来源: https://www.cnblogs.com/cathyg/p/11898258.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有