欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3的進程和線程你了解嗎

 更新時間:2022年03月15日 11:20:09   作者:FUXI_Willard  
這篇文章主要為大家詳細介紹了Python3進程和線程,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

1.概述

"""
基礎知識:
1.多任務:操作系統(tǒng)可以同時運行多個任務;
2.單核CPU執(zhí)行多任務:操作系統(tǒng)輪流讓各個任務交替執(zhí)行;
3.一個任務即一個進程(process),如:打開一個瀏覽器,即啟動一個瀏覽器進程;
4.在一個進程內,要同時干多件事,需要同時運行多個子任務,把進程內的子任務稱為"線程(Thread)";
5.每個進程至少做一件事,因此,一個進程至少有一個線程;
同時執(zhí)行多線程的解決方案:
a.啟動多個進程,每個進程雖然只有一個線程,但多個進程可以一塊執(zhí)行多個任務;
b.啟動一個進程,在一個進程內啟動多個線程,多個線程一塊執(zhí)行多個任務;
c.啟動多個進程,每個進程啟動多個線程;
即多任務的實現(xiàn)方式:
a.多進程模式;
b.多線程模式;
c.多進程+多線程模式;
"""

2.多進程

import os
print("Process (%s) start..." % os.getpid())
"""
只能在Linux/Unix/Mac上工作
pid = os.fork()
if pid == 0:
    print("I am child process (%s) and my parent is %s." % (os.getpid(), os.getppid()))
else:
    print("I (%s) just created a child process (%s)." % (os.getpid(), pid))
"""
print("Hello.")
# multiprocessing:跨平臺多線程模塊
# process_test.py文件,在交互下python process_test.py
from multiprocessing import Process
import os
def run_process(name):
    print("Run child process %s (%s)..." % (name, os.getpid()))
if __name__ == "__main__":
    print("Parent process %s." % os.getpid())
    p = Process(target = run_process, args = ("test",))
    print("Child process will start.")
    p.start()
    p.join()        # join()方法可以等待子進程結束后再繼續(xù)往下運行,用于進程間的同步
    print("Child process end.")

 # 結果輸出:
Parent process 28340.
Child process will start.
Run child process test (31152)...
Child process end.

# Pool:用進程池批量創(chuàng)建子進程
# process.py文件,交互下python process.py
from multiprocessing import Pool
import os, time, random
def long_time_task(name):
    print('Run task %s (%s)...' % (name, os.getpid()))
    start = time.time()
    time.sleep(random.random() * 3)
    end = time.time()
    print('Task %s runs %0.2f seconds.' % (name, (end - start)))
if __name__=='__main__':
    print('Parent process %s.' % os.getpid())
    p = Pool(4)
    for i in range(5):
        p.apply_async(long_time_task, args=(i,))
    print('Waiting for all subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')

# 結果輸出:
Parent process 31576.
Waiting for all subprocesses done...
Run task 0 (20416)...
Run task 1 (15900)...
Run task 2 (24716)...
Run task 3 (31148)...
Task 2 runs 0.72 seconds.
Run task 4 (24716)...
Task 4 runs 1.03 seconds.
Task 3 runs 1.82 seconds.
Task 1 runs 2.73 seconds.
Task 0 runs 2.82 seconds.
All subprocesses done.

3.子進程

# subprocess模塊:啟動一個子進程,控制其輸入和輸出
# subprocess_test.py文件,注:文件名不要和模塊名相同,否則報錯
import subprocess
print("$ nslookup www.python.org")
r = subprocess.call(["nslookup", "www.python.org"])
print("Exit code:", r)

 # 結果輸出:
$ nslookup www.python.org
服務器:  cache-a.guangzhou.gd.cn
Address:  202.96.128.86
非權威應答:
名稱:    www.python.org
Addresses:  2a04:4e42:1a::223
          151.101.72.223
Exit code: 0

# 子進程需要輸入,通過communicate()方法
import subprocess
print("$ nslookup")
p = subprocess.Popen(["nslookup"], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
output, err = p.communicate(b"set q = mx\npython.org\nexit\n")
print(output.decode("gbk"))
print("Exit code:", p.returncode)

# 結果輸出:
$ nslookup
默認服務器:  cache-a.guangzhou.gd.cn
Address:  202.96.128.86
> Unrecognized command: set q = mx
> 服務器:  cache-a.guangzhou.gd.cn
Address:  202.96.128.86
名稱:    python.org
Address:  138.197.63.241

Exit code: 0

4.進程間通信

# 在父進程中創(chuàng)建兩個子進程,一個往Queue里寫數(shù)據,一個從Queue里讀數(shù)據
# queue_test.py文件,交互下python queue_test.py
from multiprocessing import Process, Queue
import os, time, random
def write(q):
    print("Process to write:%s" % os.getpid())
    for value in ["W", "I", "L", "L", "A", "R", "D"]:
        print("Put %s to queue..." % value)
        q.put(value)
        time.sleep(random.random())
def read(q):
    print("Process to read:%s" % os.getpid())
    while True:
        value = q.get(True)
        print("Get %s from queue." % value)
if __name__ == "__main__":
    # 父進程創(chuàng)建Queue,并傳給各個子進程
    q = Queue()
    pw = Process(target = write, args = (q,))
    pr = Process(target = read, args = (q,))
    # 啟動子進程pw,寫入
    pw.start()
    # 啟動子進程pr,讀取
    pr.start()
    # 等待pw結束
    pw.join()
    # pr進程是死循環(huán),無法等待其結束,需要強行終止
    pr.terminate()

# 結果輸出:
Process to write:15720
Process to read:21524
Put W to queue...
Get W from queue.
Put I to queue...
Get I from queue.
Put L to queue...
Get L from queue.
Put L to queue...
Get L from queue.
Put A to queue...
Get A from queue.
Put R to queue...
Get R from queue.
Put D to queue...
Get D from queue.

5.多線程

# 線程庫:_thread和threading
# 啟動一個線程:即把一個函數(shù)傳入并創(chuàng)建一個Thread實例,然后調用start()開始執(zhí)行
# 任何進程默認啟動一個線程,該線程稱為主線程,主線程可以啟動新的線程
# current_thread()函數(shù):返回當前線程的實例;
# 主線程實例名字:MainThread;
# 子線程名字的創(chuàng)建時指定,如果不指定,則自動給線程命名為Thread-1、Thread-2...
import time, threading
def loop():
    print("Thread %s is running..." % threading.current_thread().name)
    n = 0
    while n < 5:
        n = n + 1
        print("Thread %s >>> %s" % (threading.current_thread().name, n))
        time.sleep(1)
    print("Thread %s ended." % threading.current_thread().name)
print("Thread %s is running..." % threading.current_thread().name)
thread1 = threading.Thread(target = loop, name = "LoopThread")
thread1.start()
thread1.join()
print("Thread %s ended." % threading.current_thread().name)

# 結果輸出:
Thread MainThread is running...
Thread LoopThread is running...
Thread LoopThread >>> 1
Thread LoopThread >>> 2
Thread LoopThread >>> 3
Thread LoopThread >>> 4
Thread LoopThread >>> 5
Thread LoopThread ended.
Thread MainThread ended.

6.Lock

# 多進程:同一個變量,各自有一份拷貝存在于每個進程中,互不影響;
# 多線程:所有變量由所有線程共享,任何一個變量可以被任何一個線程修改;
# 多線程同時操作一個變量
# 多運行幾次,發(fā)現(xiàn)結果不為0
import time, threading
balance = 0
def change_it(n):
    global balance
    balance = balance + n
    balance = balance - n
def run_thread(n):
    # 線程交替執(zhí)行,balance結果不一定為0
    for i in range(2000000):
        change_it(n)
thread1 = threading.Thread(target = run_thread, args = (5,))
thread2 = threading.Thread(target = run_thread, args = (8,))
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print(balance)
# 結果輸出:
# 5(各自不同)
# 確保balance計算正確,需要給change_it()上一把鎖
# 當線程開始執(zhí)行change_it()時,該線程獲得鎖,其他線程不能同時執(zhí)行change_it(),
# 只能等待,直到鎖被釋放,獲得該鎖后才能改;
# 通過threading.Lock()創(chuàng)建鎖
import time, threading
balance = 0
lock = threading.Lock()
def change_it(n):
    global balance
    balance = balance + n
    balance = balance - n
def run_thread(n):
    for i in range(2000000):
        lock.acquire()
        try:
            change_it(n)
        finally:
            # 釋放鎖
            lock.release()
thread1 = threading.Thread(target = run_thread, args = (5,))
thread2 = threading.Thread(target = run_thread, args = (8,))
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print(balance)
# 結果輸出:
# 0

7.ThreadLocal

# 多線程環(huán)境下,每個線程有自己的數(shù)據;
# 一個線程使用自己的局部變量比使用全局變量好;
import threading
# 創(chuàng)建全局ThreadLocal對象
local_school = threading.local()
def process_student():
    # 獲取當前線程關聯(lián)的student
    std = local_school.student
    print("Hello,%s (in %s)" % (std, threading.current_thread().name))
def process_thread(name):
    # 綁定ThreadLocal的student
    local_school.student = name
    process_student()
thread1 = threading.Thread(target = process_thread, args = ("Willard",), name = "Thread-1")
thread2 = threading.Thread(target = process_thread, args = ("WenYu",), name = "Thread-2")
thread1.start()
thread2.start()
thread1.join()
thread2.join()

# 結果輸出:
# Hello,Willard (in Thread-1)
# Hello,WenYu (in Thread-2)

8.進程VS線程

# 進程和線程優(yōu)缺點:
# 1.要實現(xiàn)多任務,會設計Master-Worker模式,Master負責分配任務,Worker負責執(zhí)行任務,
# 在多任務環(huán)境下,通常是一個Master,多個Worker;
#     a.如果使用多進程實現(xiàn)Master-Worker,主進程即Master,其他進程即Worker;
#     b.如果使用多線程實現(xiàn)Master-Worker,主線程即Master,其他線程即Worker;
# 2.多進程優(yōu)點:穩(wěn)定性高,一個子進程崩潰不會影響主進程和其他子進程;
# 3.多進程缺點:創(chuàng)建進程的代價大,操作系統(tǒng)能同時運行的進程數(shù)有限;
# 4.多線程缺點:任何一個線程崩潰,可能直接造成整個進程崩潰;
# 線程切換:
# 1.依次完成任務的方式稱為單任務模型,或批處理任務模型;
# 2.任務1先做n分鐘,切換到任務2做n分鐘,再切換到任務3做n分鐘,依此類推,稱為多任務模型;
# 計算密集型 VS IO密集型
# 1.計算密集型任務:要進行大量的計算,消耗CPU資源,如:對視頻進行高清解碼等;
# 2.IO密集型任務:涉及到網絡、磁盤IO的任務,均為IO密集型任務;
# 3.IO密集型任務消耗CPU少,大部分時間在等待IO操作完成;
# 異步IO
# 1.事件驅動模型:用單進程單線程模型來執(zhí)行多任務;
# 2.Python語言中,單線程的異步編程模型稱為協(xié)程;

9.分布式進程

"""
實例:
有一個通過Queue通信的多進程程序在同一機器上運行,但現(xiàn)在處理任務的進程任務繁重,
希望把發(fā)送任務的進程和處理任務的進程發(fā)布到兩臺機器上;
"""
# task_master_test.py
# 交互環(huán)境中:python task_master_test.py
import random, time, queue
from multiprocessing.managers import BaseManager
# 發(fā)送任務的隊列
task_queue = queue.Queue()
# 接收結果的隊列
result_queue = queue.Queue()
def return_task_queue():
    global task_queue
    return task_queue
def return_result_queue():
    global task_queue
    return task_queue
# 從BaseManager繼承的QueueManager
class QueueManager(BaseManager):
    pass
if __name__ == "__main__":
    # 把兩個Queue注冊到網絡上,callable參數(shù)關聯(lián)Queue對象
    QueueManager.register("get_task_queue", callable = return_task_queue)
    QueueManager.register("get_result_queue", callable = return_result_queue)
    # 綁定端口5000,設置驗證碼"Willard"
    manager = QueueManager(address = ("127.0.0.1", 5000), authkey = b"Willard")
    # 啟動Queue
    manager.start()
    # 獲得通過網絡訪問的Queue對象
    task = manager.get_task_queue()
    result = manager.get_result_queue()
    # 放任務進去
    for i in range(10):
        n = random.randint(0, 10000)
        print("Put task %d..." % n)
        task.put(n)
    # 從result隊列讀取結果
    print("Try get results...")
    for i in range(10):
        r = result.get(timeout = 10)
        print("Result:%s" % r)
    # 關閉
    manager.shutdown()
    print("Master Exit.")
# task_worker_test.py文件
# 交互環(huán)境python task_worker_test.py
import time, sys, queue
from multiprocessing.managers import BaseManager
# 創(chuàng)建QueueManager
class QueueManager(BaseManager):
    pass
QueueManager.register("get_task_queue")
QueueManager.register("get_result_queue")
# 連接到服務器
server_address = "127.0.0.1"
print("Connect to server %s..." % server_address)
# 端口和驗證碼
m = QueueManager(address = (server_address, 5000), authkey = b"Willard")
# 網絡連接
m.connect()
# 獲取Queue對象
task = m.get_task_queue()
result = m.get_result_queue()
# 從task隊列取任務,把結果寫入result隊列
for i in range(10):
    try:
        n = task.get(timeout = 1)
        print("Run task %d * %d..." % (n, n))
        r = "%d * %d = %d" % (n, n, n * n)
        time.sleep(1)
        result.put(r)
    except Queue.Empty:
        print("Task queue is empty.")
print("Worker Exit.")

總結

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

相關文章

  • Python中字符串格式化的方法小結

    Python中字符串格式化的方法小結

    在Python中,格式化字符串輸出是一項非常常見的任務,Python提供了多種方式來實現(xiàn)字符串格式化,每種方式都有其獨特的優(yōu)勢和用法,下面我們就來學習一下這些方法的具體操作吧
    2023-11-11
  • python修改list中所有元素類型的三種方法

    python修改list中所有元素類型的三種方法

    下面小編就為大家分享一篇python修改list中所有元素類型的三種方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python中從str中提取元素到list以及將list轉換為str的方法

    python中從str中提取元素到list以及將list轉換為str的方法

    今天小編就為大家分享一篇python中從str中提取元素到list以及將list轉換為str的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python?Panda中索引和選擇?series?的數(shù)據

    Python?Panda中索引和選擇?series?的數(shù)據

    這篇文章主要介紹了Python?Panda中索引和選擇series的數(shù)據,文章通過圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Python創(chuàng)建二維數(shù)組實例(關于list的一個小坑)

    Python創(chuàng)建二維數(shù)組實例(關于list的一個小坑)

    下面小編就為大家?guī)硪黄狿ython創(chuàng)建二維數(shù)組實例(關于list的一個小坑)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • 基于Python實現(xiàn)語音合成小工具

    基于Python實現(xiàn)語音合成小工具

    TTS(Text To Speech)是一種語音合成技術,可以讓機器將輸入文本以語音的方式播放出來,實現(xiàn)機器說話的效果。本文將使用pyttsx3庫作為示范,編寫一個語音合成小工具,感興趣的可以了解一下
    2022-12-12
  • 詳解基于K-means的用戶畫像聚類模型

    詳解基于K-means的用戶畫像聚類模型

    這篇文章主要介紹了基于K-means的用戶畫像聚類模型,本文中就是使用one-hot思想將不同維度的數(shù)據利用字典映射的方式將其轉化為數(shù)據向量,需要的朋友可以參考下
    2022-05-05
  • python+appium+yaml移動端自動化測試框架實現(xiàn)詳解

    python+appium+yaml移動端自動化測試框架實現(xiàn)詳解

    這篇文章主要介紹了python+appium+yaml移動端自動化測試框架實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • python特殊字符作為字符串不轉義的問題

    python特殊字符作為字符串不轉義的問題

    這篇文章主要介紹了python特殊字符作為字符串不轉義的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • jupyter安裝小結

    jupyter安裝小結

    jupyter (之前的 ipython notebook )于我的最大意義在于,讓學習進程和探索進程變得可累積,正如它的原先名字中的 notebook 所暗示的那樣,作為學習的記錄者,方便你隨時撿起學習的進度,增量式地前進
    2016-03-03

最新評論