一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 手把手帶你了解python多進程,多線程

手把手帶你了解python多進程,多線程

2021-12-22 00:37Mr DaYang Python

這篇文章主要介紹了python多線程與多進程及其區別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

說明

相應的學習視頻見鏈接,本文只對重點進行總結。

手把手帶你了解python多進程,多線程

手把手帶你了解python多進程,多線程

多進程

重點(只要看下面代碼的main函數即可)

1.創建

2.如何開守護進程

3.多進程,開銷大,用for循環調用多個進程時,后臺cpu一下就上去了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import time
import multiprocessing
import os
def dance(who,num):
    print("dance父進程:{}".format(os.getppid()))
    for i in range(1,num+1):
        print("進行編號:{}————{}跳舞。。。{}".format(os.getpid(),who,i))
        time.sleep(0.5)
def sing(num):
    print("sing父進程:{}".format(os.getppid()))
    for i in range(1,num+1):
        print("進行編號:{}----唱歌。。。{}".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主進程{}".format(os.getpid()))
    start= time.time()
    #1 進程的創建與啟動
    # # 1.1創建進程對象,注意dance不能加括號
    # # dance_process = multiprocessing.process(target=dance)#1.無參數
    # dance_process=multiprocessing.process(target=dance,args=("lin",3))#2.以args=元祖方式
    # sing_process = multiprocessing.process(target=sing,kwargs={"num":3})#3.以kwargs={}字典方式
    # # 1.2啟動進程
    # dance_process.start()
    # sing_process.start()
    #2.默認-主進程和子進程是分開的,主進程只要1s就可以完成,子進程要2s,主進程會等所有子進程執行完,再退出
    # 2.1子守護主進程,當主一但完成,子就斷開(如qq一關閉,所有聊天窗口就沒了).daemon=true
    work_process = multiprocessing.process(target=work,daemon=true)
    work_process.start()
    time.sleep(1)
    print("主進程完成了!")#主進程和子進程是分開的,主進程只要1s就可以完成,子進程要2s,主進程會等所有子進程執行完,再退出
    print("main主進程花費時長:",time.time()-start)
    #

多線程

手把手帶你了解python多進程,多線程

重點

1.創建

2.守護線程

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import time
import os
import threading
def dance(num):
    for i in range(num):
        print("進程編號:{},線程編號:{}————跳舞。。。".format(os.getpid(),threading.current_thread()))
        time.sleep(1)
def sing(count):
    for i in range(count):
        print("進程編號:{},線程編號:{}----唱歌。。。".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)#設置成守護主線程
    # 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("進程編號:{}主線程結束...用時{}".format(os.getpid(),(time.time()-start)))
    for i in range(10):#多線程之間執行是無序的,由cpu調度
        sub_thread = threading.thread(target=task)
        sub_thread.start()

線程安全

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

手把手帶你了解python多進程,多線程
手把手帶你了解python多進程,多線程

#下面代碼在沒有lock鎖時,會賣出0票,加上lock就正常

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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()

高并發拷貝(多進程,多線程)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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("當前進程為:{}".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("目標文件已存在")
    file_list =os.listdir(source_dir)
    count=0
    #1多進程
    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)
#這里有主進程和子進程,通過打印可以看出,主進程在創建1,2,3,4,,,21過程中,子進程已有的開始執行,也就是說,每個進程是互不影響的
# 9
# 10
# 11
# 12
# 13
# 當前進程為:2936(當主進程創建第13個時,此時,第一個子進程開始工作)
# 14
# 當前進程為:10120
# 當前進程為:10440
# 15
# 當前進程為: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)

總結

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關注服務器之家的更多內容!

原文鏈接:https://blog.csdn.net/m0_46204224/article/details/119737937

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产在线成人a | 天天躁夜夜躁很很躁 | 精品国产91高清在线观看 | 美女舒服好紧太爽了视频 | 精品欧美一区二区三区久久久 | 99精品视频在线观看免费 | 日韩视频免费观看 | 亚洲高清国产拍精品影院 | 亚洲成人77777 | 四色6677最新永久网站 | 久久久久久久久人体 | 边摸边吃奶边做爽gif动态图 | 91网红福利精品区一区二 | 国产精品久久久久久 | 国产成人在线免费视频 | 色婷婷天天综合在线 | 小早川怜子息梦精在线播放 | 国产91精品区| 农村妇女野外性生话免费视频 | 欧美精品久久久久久久免费观看 | 荡娃艳妇有声小说 | 欧美video丝袜连裤袜bd | 精品免费久久久久久成人影院 | 久久内在线视频精品mp4 | 苍井空50分钟无码 | 国产午夜免费不卡精品理论片 | 日本精品中文字幕在线播放 | 亚洲国产区男人本色在线观看欧美 | 99热久久这里只有精品6国产网 | 双性太子 | 日本高清视频网站 | 人皮高跟鞋在线观看 | 福利视频一区二区牛牛 | 99久久99久久免费精品蜜桃 | 91一个人的在线观看www | 免费观看视频在线 | 国产欧美日韩综合 | 亚洲电影第1页 | 精品国产成人高清在线 | 欧美高清免费一级在线 | 人与善交大片免费看 |