python多線程、多進程

進程:一個進程就是一個程序
線程:就是進程裏面最小的執行單元
幹活的是線程,一個進程裏面有多個線程,最少有1個線程,每一個線程之間都是互相獨立的
沒有真正意義上的併發,電腦幾核 就能一塊兒運行幾個程序,由於cpu處理速度快,看起來像併發的
python裏面的多線程,是利用不了多核cpu的,只能利用一個核心cpu,GIL 全局解釋器鎖(自行百度)
有些狀況下,你用多線程,比用單線程還慢
多進程,它是能夠利用多核cpu的python

import threading,time
all_res = [] #定義全局變量,獲取函數返回值,不能用return

def run(name):
    print('子線程',threading.current_thread())
    time.sleep(1)#等待1s
    print('【%s】haha'%name)
    name = 'hhk'+name
    all_res.append(name)
threads = []#存放全部的子線程
for i in range(5):
    t = threading.Thread(target=run,args=(str(i),)) #找線程幹活 須要傳參則args  元祖傳參,只有一個參數後面須要有個逗號
    t.start() #開始幹活
    threads.append(t)
    # t.join()#等待主進程,咱們一塊兒走
print(threads)#優先打印,線程之間獨立,主進程繼續運行,可經過join讓子線程等待主線程一塊兒

也能夠經過判斷目前活躍線程數來判斷子線程是否含在運行網絡

import threading,time
all_res = [] #定義全局變量,獲取函數返回值,不能用return

def run(name):
    print('子線程',threading.current_thread())
    time.sleep(1)#等待1s
    print('【%s】haha'%name)
    name = 'hhk'+name
    all_res.append(name)
threads = []#存放全部的子線程
for i in range(5):
    t = threading.Thread(target=run,args=(str(i),)) #找線程幹活 須要傳參則args  元祖傳參,只有一個參數後面須要有個逗號
    t.start() #開始幹活
    threads.append(t)
while threading.active_count()!=1:#活躍線程數不等於1,就等,直到只剩主線程
    pass
print(threads)

線程池多線程

import threadpool,pymongo,requests#第三方模塊
client = pymongo.MongoClient(host='118.24.3.xx')
table = client['likun']['qq_group_likun']
all_qq = [i.get('qq') for i in table.find()]
print(all_qq)


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

def down_img(qq_num):
    res = requests.get(url%qq_num).content
    with open('%s.jpg'%qq_num, 'wb') as fw:
        fw.write(res)
pool = threadpool.ThreadPool(200)#線程池的大小
all_request = threadpool.makeRequests(down_img,all_qq)#分配數據
for a in all_request:
    pool.putRequest(a)#往線程池中加請求
# [pool.putRequest(a) for a in all_request]
pool.wait()#等待全部的線程運行完

多進程、進程池併發

#何時用多線程,什麼是時候用多進程
# 多線程適用於io密集型任務
#     磁盤io,網絡io
# 多進程適用於cpu密集型任務
    # 排序
from multiprocessing import Process,Pool,active_children
import pymongo,requests
client = pymongo.MongoClient(host='118.24.3.xx',port=27017)
table = client['likun']['qq_group_likun']
all_qq = [i.get('qq') for i in table.find()]

url = 'http://q4.qlogo.cn/g?b=qq&nk=%s&s=140'
def down_img(qq_num):
    res = requests.get(url%qq_num).content
    with open('%s.jpg'%qq_num,'wb') as fw:
        fw.write(res)



if __name__ == '__main__':
    # for qq in all_qq:
    #     p = Process(target=down_img,args=(qq,))#多進程
    #     p.start()
    # # print(active_children())
    #
    pool = Pool(20)#進程池大小
    list(pool.map(down_img,all_qq))#運行。使用進程池

app

import threading
from threading import Lock
num = 0
lock = Lock()#實例化一把鎖,
#多個線程操做一份數據,最好加上鎖

def run():
    global num
    # lock.acquire()#枷鎖
    # num+=1
    # lock.release()#解鎖
    while num<1000:
        with lock: #自動解鎖了
            num+=1
for i in range(100):
    t = threading.Thread(target=run)
    t.start()

print(num)
相關文章
相關標籤/搜索