成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Python多線程編程全解析:基礎到高級用法

開發 后端
Python的Threading模塊提供了多線程編程的基本工具。在下面,我將列舉一些基礎的多線程用法和一些高級用法,并提供相應的源代碼,其中包含中文注釋。

Python中有多線程的支持。Python的threading模塊提供了多線程編程的基本工具。在下面,我將列舉一些基礎的多線程用法和一些高級用法,并提供相應的源代碼,其中包含中文注釋。

基礎用法:

創建和啟動線程

import threading
import time

# 定義一個簡單的線程類
class MyThread(threading.Thread):
    def run(self):
        for _ in range(5):
            print(threading.current_thread().name, "is running")
            time.sleep(1)

# 創建兩個線程實例
thread1 = MyThread(name="Thread-1")
thread2 = MyThread(name="Thread-2")

# 啟動線程
thread1.start()
thread2.start()

# 主線程等待所有子線程結束
thread1.join()
thread2.join()

print("Main thread exiting")

線程同步 - 使用鎖

import threading

# 共享資源
counter = 0

# 創建鎖
counter_lock = threading.Lock()

# 定義一個簡單的線程類
class MyThread(threading.Thread):
    def run(self):
        global counter
        for _ in range(5):
            with counter_lock:  # 使用鎖保護臨界區
                counter += 1
                print(threading.current_thread().name, "Counter:", counter)

# 創建兩個線程實例
thread1 = MyThread(name="Thread-1")
thread2 = MyThread(name="Thread-2")

# 啟動線程
thread1.start()
thread2.start()

# 主線程等待所有子線程結束
thread1.join()
thread2.join()

print("Main thread exiting")

高級用法:

使用線程池

import concurrent.futures
import time

# 定義一個簡單的任務函數
def task(name):
    print(f"{name} is running")
    time.sleep(2)
    return f"{name} is done"

# 使用線程池
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
    # 提交任務給線程池
    future_to_name = {executor.submit(task, f"Thread-{i}"): f"Thread-{i}" for i in range(5)}

    # 獲取任務結果
    for future in concurrent.futures.as_completed(future_to_name):
        name = future_to_name[future]
        try:
            result = future.result()
            print(f"{name}: {result}")
        except Exception as e:
            print(f"{name}: {e}")

使用Condition進行線程間通信

import threading
import time

# 共享資源
shared_resource = None

# 創建條件變量
condition = threading.Condition()

# 定義一個寫線程
class WriterThread(threading.Thread):
    def run(self):
        global shared_resource
        for _ in range(5):
            with condition:
                shared_resource = "Write data"
                print("Writer wrote:", shared_resource)
                condition.notify()  # 通知等待的線程
                condition.wait()  # 等待其他線程通知

# 定義一個讀線程
class ReaderThread(threading.Thread):
    def run(self):
        global shared_resource
        for _ in range(5):
            with condition:
                while shared_resource is None:
                    condition.wait()  # 等待寫線程通知
                print("Reader read:", shared_resource)
                shared_resource = None
                condition.notify()  # 通知寫線程

# 創建寫線程和讀線程
writer_thread = WriterThread()
reader_thread = ReaderThread()

# 啟動線程
writer_thread.start()
reader_thread.start()

# 主線程等待所有子線程結束
writer_thread.join()
reader_thread.join()

print("Main thread exiting")

這些例子涵蓋了一些基礎和高級的多線程用法。請注意,在Python中由于全局解釋器鎖(GIL)的存在,多線程并不能充分利用多核處理器。如果需要充分利用多核處理器,可以考慮使用multiprocessing模塊進行多進程編程。

責任編輯:姜華 來源: 今日頭條
相關推薦

2023-10-27 08:42:56

Python字典

2009-03-12 10:52:43

Java線程多線程

2011-06-13 10:41:17

JAVA

2025-02-08 07:00:00

2023-06-12 08:30:08

多線程編程線程調試

2021-03-01 11:20:13

網絡安全多線程代碼

2017-03-08 14:18:37

Linux多線程編程

2011-07-01 17:50:13

Python 多線程

2021-08-12 14:33:20

Python多線程編程

2013-07-16 10:12:14

iOS多線程多線程概念多線程入門

2011-06-22 16:18:23

QT 多線程 QSocket

2023-06-13 13:39:00

多線程異步編程

2011-06-02 17:27:49

iphone 多線程

2010-03-03 17:44:07

Python多線程

2023-10-18 15:19:56

2024-10-16 09:34:50

2023-04-02 17:53:10

多線程編程自測

2023-06-07 13:49:00

多線程編程C#

2023-06-05 07:56:10

線程分配處理器

2023-06-06 08:17:52

多線程編程Thread類
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品久久一区二区三区 | 成人精品一区二区三区 | 亚洲欧美少妇 | 中文字幕一区在线 | 成人网视频| 国产免费一级一级 | 国产日产欧产精品精品推荐蛮挑 | www国产成人 | 亚洲毛片在线观看 | 久久9精品| 日韩网站在线观看 | 欧洲av一区 | 超黄视频网站 | 免费一区 | 美日韩免费视频 | 不卡一区二区三区四区 | 美女国产 | 国产精品视频一区二区三区四区国 | 久久久久久久久久久高潮一区二区 | 天天精品在线 | 免费黄色在线观看 | 动漫www.被爆羞羞av44 | 一区二区三区视频在线观看 | 天天干天天操天天看 | 亚洲综合五月天婷婷 | 国产黄色小视频在线观看 | 香蕉国产在线视频 | 在线免费观看黄a | 91私密视频 | 91精品在线看 | 久久精品视频12 | 91看片官网 | 国产精品69av| 日日干综合 | 国产欧美一区二区三区在线看 | av黄色网| 亚洲成人免费在线观看 | 欧美激情国产日韩精品一区18 | 免费观看一区二区三区毛片 | 伊人精品一区二区三区 | 日韩色图在线观看 |