最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

手把手帶你了解python多進(jìn)程,多線程

 更新時(shí)間:2021年08月19日 16:44:39   作者:Mr DaYang  
這篇文章主要介紹了python多線程與多進(jìn)程及其區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

說明

相應(yīng)的學(xué)習(xí)視頻見鏈接,本文只對(duì)重點(diǎn)進(jìn)行總結(jié)。

請(qǐng)?zhí)砑訄D片描述

請(qǐng)?zhí)砑訄D片描述

多進(jìn)程

重點(diǎn)(只要看下面代碼的main函數(shù)即可)

1.創(chuàng)建

2.如何開守護(hù)進(jìn)程

3.多進(jìn)程,開銷大,用for循環(huán)調(diào)用多個(gè)進(jìn)程時(shí),后臺(tái)cpu一下就上去了

import time
import multiprocessing
import os
def dance(who,num):
    print("dance父進(jìn)程:{}".format(os.getppid()))
    for i in range(1,num+1):
        print("進(jìn)行編號(hào):{}————{}跳舞。。。{}".format(os.getpid(),who,i))
        time.sleep(0.5)
def sing(num):
    print("sing父進(jìn)程:{}".format(os.getppid()))
    for i in range(1,num+1):
        print("進(jìn)行編號(hào):{}----唱歌。。。{}".format(os.getpid(),i))
        time.sleep(0.5)
def work():
    for i in range(10):
        print("工作中。。。")
        time.sleep(0.2)
if __name__ == '__main__':
    # print("main主進(jìn)程{}".format(os.getpid()))
    start= time.time()
    #1 進(jìn)程的創(chuàng)建與啟動(dòng)
    # # 1.1創(chuàng)建進(jìn)程對(duì)象,注意dance不能加括號(hào)
    # # dance_process = multiprocessing.Process(target=dance)#1.無參數(shù)
    # dance_process=multiprocessing.Process(target=dance,args=("lin",3))#2.以args=元祖方式
    # sing_process = multiprocessing.Process(target=sing,kwargs={"num":3})#3.以kwargs={}字典方式
    # # 1.2啟動(dòng)進(jìn)程
    # dance_process.start()
    # sing_process.start()
    #2.默認(rèn)-主進(jìn)程和子進(jìn)程是分開的,主進(jìn)程只要1s就可以完成,子進(jìn)程要2s,主進(jìn)程會(huì)等所有子進(jìn)程執(zhí)行完,再退出
    # 2.1子守護(hù)主進(jìn)程,當(dāng)主一但完成,子就斷開(如qq一關(guān)閉,所有聊天窗口就沒了).daemon=True
    work_process = multiprocessing.Process(target=work,daemon=True)
    work_process.start()
    time.sleep(1)
    print("主進(jìn)程完成了!")#主進(jìn)程和子進(jìn)程是分開的,主進(jìn)程只要1s就可以完成,子進(jìn)程要2s,主進(jìn)程會(huì)等所有子進(jìn)程執(zhí)行完,再退出
    print("main主進(jìn)程花費(fèi)時(shí)長:",time.time()-start)
    #

多線程

請(qǐng)?zhí)砑訄D片描述

重點(diǎn)

1.創(chuàng)建

2.守護(hù)線程

3.線程安全問題(多人搶票,會(huì)搶到同一張)

import time
import os
import threading
def dance(num):
    for i in range(num):
        print("進(jìn)程編號(hào):{},線程編號(hào):{}————跳舞。。。".format(os.getpid(),threading.current_thread()))
        time.sleep(1)
def sing(count):
    for i in range(count):
        print("進(jìn)程編號(hào):{},線程編號(hào):{}----唱歌。。。".format(os.getpid(),threading.current_thread()))
        time.sleep(1)
def task():
    time.sleep(1)
    thread=threading.current_thread()
    print(thread)
if __name__ == '__main__':
    # start=time.time()
    # # sing_thread =threading.Thread(target=dance,args=(3,),daemon=True)#設(shè)置成守護(hù)主線程
    # sing_thread = threading.Thread(target=dance, args=(3,))
    # dance_thread = threading.Thread(target=sing,kwargs={"count":3})
    #
    # sing_thread.start()
    # dance_thread.start()
    #
    # time.sleep(1)
    # print("進(jìn)程編號(hào):{}主線程結(jié)束...用時(shí){}".format(os.getpid(),(time.time()-start)))
    for i in range(10):#多線程之間執(zhí)行是無序的,由cpu調(diào)度
        sub_thread = threading.Thread(target=task)
        sub_thread.start()

線程安全

由于線程直接是無序進(jìn)行的,且他們共享同一個(gè)進(jìn)程的全部資源,所以會(huì)產(chǎn)生線程安全問題(比如多人在線搶票,買到同一張)

請(qǐng)?zhí)砑訄D片描述
請(qǐng)?zhí)砑訄D片描述

#下面代碼在沒有l(wèi)ock鎖時(shí),會(huì)賣出0票,加上lock就正常

import threading
import time
lock =threading.Lock()
class Sum_tickets:
    def __init__(self,tickets):
        self.tickets=tickets
def window(sum_tickets):
    while True:
        with lock:
            if sum_tickets.tickets>0:
                time.sleep(0.2)
                print(threading.current_thread().name,"取票{}".format(sum_tickets.tickets))
                sum_tickets.tickets-=1
            else:
                break
if __name__ == '__main__':
    sum_tickets=Sum_tickets(10)
    sub_thread1 = threading.Thread(name="窗口1",target=window,args=(sum_tickets,))
    sub_thread2 = threading.Thread(name="窗口2",target=window,args=(sum_tickets,))
    sub_thread1.start()
    sub_thread2.start()

高并發(fā)拷貝(多進(jìn)程,多線程)

import os
import multiprocessing
import threading
import time
def copy_file(file_name,source_dir,dest_dir):
    source_path = source_dir+"/"+file_name
    dest_path =dest_dir+"/"+file_name
    print("當(dāng)前進(jìn)程為:{}".format(os.getpid()))
    with open(source_path,"rb") as source_file:
        with open(dest_path,"wb") as dest_file:
            while True:
                data=source_file.read(1024)
                if data:
                    dest_file.write(data)
                else:
                    break
    pass
if __name__ == '__main__':
    source_dir=r'C:\Users\Administrator\Desktop\注意力'
    dest_dir=r'C:\Users\Administrator\Desktop\test'
    start = time.time()
    try:
        os.mkdir(dest_dir)
    except:
        print("目標(biāo)文件已存在")
    file_list =os.listdir(source_dir)
    count=0
    #1多進(jìn)程
    for file_name in file_list:
        count+=1
        print(count)
        sub_processor=multiprocessing.Process(target=copy_file,
                                args=(file_name,source_dir,dest_dir))
        sub_processor.start()
        # time.sleep(20)
    print(time.time()-start)
#這里有主進(jìn)程和子進(jìn)程,通過打印可以看出,主進(jìn)程在創(chuàng)建1,2,3,4,,,21過程中,子進(jìn)程已有的開始執(zhí)行,也就是說,每個(gè)進(jìn)程是互不影響的
# 9
# 10
# 11
# 12
# 13
# 當(dāng)前進(jìn)程為:2936(當(dāng)主進(jìn)程創(chuàng)建第13個(gè)時(shí),此時(shí),第一個(gè)子進(jìn)程開始工作)
# 14
# 當(dāng)前進(jìn)程為:10120
# 當(dāng)前進(jìn)程為:10440
# 15
# 當(dāng)前進(jìn)程為:9508
    # 2多線程
    # for file_name in file_list:
    #     count += 1
    #     print(count)
    #     sub_thread = threading.Thread(target=copy_file,
    #                                             args=(file_name, source_dir, dest_dir))
    #     sub_thread.start()
    #     # time.sleep(20)
    # print(time.time() - start)

總結(jié)

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • Python+requests+unittest執(zhí)行接口自動(dòng)化測(cè)試詳情

    Python+requests+unittest執(zhí)行接口自動(dòng)化測(cè)試詳情

    這篇文章主要介紹了Python+requests+unittest執(zhí)行接口自動(dòng)化測(cè)試詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-09-09
  • Python中隨機(jī)數(shù)函數(shù)的5個(gè)核心工具全解析

    Python中隨機(jī)數(shù)函數(shù)的5個(gè)核心工具全解析

    隨機(jī)數(shù)在編程中無處不在,從游戲開發(fā)到機(jī)器學(xué)習(xí),從密碼學(xué)到統(tǒng)計(jì)模擬,本文將深入解析5個(gè)最實(shí)用的隨機(jī)數(shù)函數(shù),有需要的小伙伴可以了解下
    2025-09-09
  • Python如何識(shí)別銀行卡卡號(hào)?

    Python如何識(shí)別銀行卡卡號(hào)?

    今天給大家?guī)淼氖怯嘘P(guān)Python的相關(guān)知識(shí),文章圍繞著Python如何識(shí)別銀行卡卡號(hào)展開,文中有非常詳細(xì)的代碼示例及介紹,需要的朋友可以參考下
    2021-06-06
  • Python多線程編程(一):threading模塊綜述

    Python多線程編程(一):threading模塊綜述

    這篇文章主要介紹了Python多線程編程(一):threading模塊綜述,本文講解了threading模塊、Thread類、Queue提供的類等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • python 已知三條邊求三角形的角度案例

    python 已知三條邊求三角形的角度案例

    這篇文章主要介紹了python 已知三條邊求三角形的角度案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python對(duì)指定目錄下文件進(jìn)行批量重命名的方法

    python對(duì)指定目錄下文件進(jìn)行批量重命名的方法

    這篇文章主要介紹了python對(duì)指定目錄下文件進(jìn)行批量重命名的方法,涉及Python中replace及join方法的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • 詳解Python爬取并下載《電影天堂》3千多部電影

    詳解Python爬取并下載《電影天堂》3千多部電影

    這篇文章主要介紹了Python爬取并下載《電影天堂》3千多部電影,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • django中row語法詳解

    django中row語法詳解

    在Django模板中,使用{{ row }}語法可以輸出列表row的所有元素,但是如果你想要輸出列表中的某個(gè)元素,需要使用下標(biāo)來訪問它,這篇文章主要介紹了django中row語法詳解,需要的朋友可以參考下
    2023-06-06
  • python執(zhí)行數(shù)據(jù)庫的查詢操作實(shí)例講解

    python執(zhí)行數(shù)據(jù)庫的查詢操作實(shí)例講解

    在本篇文章里小編給大家整理了一篇關(guān)于python執(zhí)行數(shù)據(jù)庫的查詢操作實(shí)例講解內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。
    2021-10-10
  • python中的列表與元組的使用

    python中的列表與元組的使用

    這篇文章主要介紹了python中的列表與元組的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08

最新評(píng)論

沿河| 上高县| 峡江县| 泾川县| 元氏县| 常熟市| 新竹县| 黔东| 若尔盖县| 大足县| 金堂县| 徐汇区| 融水| 邻水| 乌拉特中旗| 积石山| 陆良县| 洞头县| 大同县| 三台县| 嫩江县| 大姚县| 扬州市| 呼图壁县| 辉县市| 墨玉县| 新化县| 临夏市| 正蓝旗| 建宁县| 五指山市| 隆回县| 贡觉县| 天祝| 涿州市| 连城县| 石狮市| 莲花县| 遂平县| 丹江口市| 额尔古纳市|