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

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.啟動多個進程,每個進程啟動多個線程;
即多任務的實現方式:
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里寫數據,一個從Queue里讀數據
# 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
# 啟動一個線程:即把一個函數傳入并創(chuàng)建一個Thread實例,然后調用start()開始執(zhí)行
# 任何進程默認啟動一個線程,該線程稱為主線程,主線程可以啟動新的線程
# current_thread()函數:返回當前線程的實例;
# 主線程實例名字: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ā)現結果不為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)境下,每個線程有自己的數據;
# 一個線程使用自己的局部變量比使用全局變量好;
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.要實現多任務,會設計Master-Worker模式,Master負責分配任務,Worker負責執(zhí)行任務,
# 在多任務環(huán)境下,通常是一個Master,多個Worker;
#     a.如果使用多進程實現Master-Worker,主進程即Master,其他進程即Worker;
#     b.如果使用多線程實現Master-Worker,主線程即Master,其他線程即Worker;
# 2.多進程優(yōu)點:穩(wěn)定性高,一個子進程崩潰不會影響主進程和其他子進程;
# 3.多進程缺點:創(chuàng)建進程的代價大,操作系統(tǒng)能同時運行的進程數有限;
# 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通信的多進程程序在同一機器上運行,但現在處理任務的進程任務繁重,
希望把發(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參數關聯(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.")

總結

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

相關文章

  • tornado+celery的簡單使用詳解

    tornado+celery的簡單使用詳解

    今天小編就為大家分享一篇tornado+celery的簡單使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python生成二維碼的實例詳解

    python生成二維碼的實例詳解

    這篇文章主要介紹了python生成二維碼的實例詳解的相關資料,希望通過本文能幫助到大家,讓大家實現這樣的功能,需要的朋友可以參考下
    2017-10-10
  • 基于scrapy實現的簡單蜘蛛采集程序

    基于scrapy實現的簡單蜘蛛采集程序

    這篇文章主要介紹了基于scrapy實現的簡單蜘蛛采集程序,實例分析了scrapy實現采集程序的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • python實現人工智能Ai摳圖功能

    python實現人工智能Ai摳圖功能

    這篇文章主要介紹了python實現人工智能Ai摳圖功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • python中使用enumerate函數遍歷元素實例

    python中使用enumerate函數遍歷元素實例

    這篇文章主要介紹了python中使用enumerate函數遍歷元素實例,這是一個比較簡單的例子,需要的朋友可以參考下
    2014-06-06
  • Python實現圖片拼接的代碼

    Python實現圖片拼接的代碼

    本文通過實例代碼給大家介紹了python實現圖片拼接的方法,非常不錯,具有一定的參考借鑒借鑒價值,需要的朋友參考下吧
    2018-07-07
  • Python IDLE入門簡介

    Python IDLE入門簡介

    這篇文章主要介紹了Python IDLE入門簡介,具有一定借鑒價值,需要的朋友可以了解下。
    2017-12-12
  • 如何在Windows下載、安裝Python和配置環(huán)境(新手、保姆級教程)

    如何在Windows下載、安裝Python和配置環(huán)境(新手、保姆級教程)

    本文詳細介紹了如何在Windows系統(tǒng)上下載、安裝Python以及配置環(huán)境變量,通過步驟說明,即使是新手也能順利完成Python的安裝,并驗證其是否成功,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • Python模塊搜索路徑代碼詳解

    Python模塊搜索路徑代碼詳解

    這篇文章主要介紹了Python模塊搜索路徑代碼詳解,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • python3.7將代碼打包成exe程序并添加圖標的方法

    python3.7將代碼打包成exe程序并添加圖標的方法

    這篇文章主要介紹了python3.7將代碼打包成exe程序并添加圖標的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-10-10

最新評論

磐安县| 莱州市| 新闻| 蓬溪县| 天台县| 达州市| 运城市| 柘城县| 麦盖提县| 青川县| 宁远县| 沁水县| 家居| 白银市| 东海县| 深水埗区| 明溪县| 葵青区| 云阳县| 来安县| 托克逊县| 陆丰市| 成都市| 徐汇区| 景泰县| 青岛市| 新和县| 仙游县| 定西市| 扶绥县| 德化县| 开江县| 固始县| 措勤县| 翁牛特旗| 丽江市| 隆化县| 舟曲县| 嘉义县| SHOW| 泗阳县|