Python线程使用

使用 time.sleep 来模拟 I/O 密集型任务(如爬取数据),并展示如何通过线程实现这些任务的并发执行。为了更清楚地体现出线程可以同步进行多个任务,将打印出每个任务的状态和时间戳。 

import threading
import time


# 模拟从网站获取数据的任务
def fetch_website(url, delay):
    print(f"{time.ctime()} - Fetching {url}...")
    time.sleep(delay)  # 模拟网络请求的延迟
    print(f"{time.ctime()} - Fetched {url}")


# 使用线程并发获取多个网站的数据
def fetch_websites(urls, delays):
    threads = []

    for url, delay in zip(urls, delays):
        thread = threading.Thread(target=fetch_website, args=(url, delay))
        print(f"{time.ctime()} - Starting thread for {url}...")
        threads.append(thread)
        print(f"{time.ctime()} - Thread {thread.name} started.")
        thread.start()
        print(f"{time.ctime()} - Thread {thread.name} started.")

    # 等待所有线程完成
    for thread in threads:
        print("Waiting for all threads...")
        thread.join(15)
        print(f"{time.ctime()} - Thread {thread.name} completed.")


if __name__ == "__main__":
    start_time = time.time()

    urls = [
        "https://www.example.com",
        "https://www.python.org",
        "https://www.github.com",
        "https://www.wikipedia.org"
    ]

    delays = [5, 20, 3, 10]  # 每个任务的不同延迟时间

    print(f"{time.ctime()} - Starting to fetch websites...")
    fetch_websites(urls, delays)
    end_time = time.time()

    print(f"{time.ctime()} - All websites fetched.")
    print(f"Total time taken: {end_time - start_time:.2f} seconds")

Sun Dec  1 12:40:59 2024 - Starting to fetch websites...
Sun Dec  1 12:40:59 2024 - Starting thread for https://www.example.com...
Sun Dec  1 12:40:59 2024 - Thread Thread-1 (fetch_website) started.
Sun Dec  1 12:40:59 2024 - Fetching https://www.example.com...Sun Dec  1 12:40:59 2024 - Thread Thread-1 (fetch_website) started.

Sun Dec  1 12:40:59 2024 - Starting thread for https://www.python.org...
Sun Dec  1 12:40:59 2024 - Thread Thread-2 (fetch_website) started.
Sun Dec  1 12:40:59 2024 - Fetching https://www.python.org...Sun Dec  1 12:40:59 2024 - Thread Thread-2 (fetch_website) started.

Sun Dec  1 12:40:59 2024 - Starting thread for https://www.github.com...
Sun Dec  1 12:40:59 2024 - Thread Thread-3 (fetch_website) started.
Sun Dec  1 12:40:59 2024 - Fetching https://www.github.com...Sun Dec  1 12:40:59 2024 - Thread Thread-3 (fetch_website) started.

Sun Dec  1 12:40:59 2024 - Starting thread for https://www.wikipedia.org...
Sun Dec  1 12:40:59 2024 - Thread Thread-4 (fetch_website) started.
Sun Dec  1 12:40:59 2024 - Fetching https://www.wikipedia.org...Sun Dec  1 12:40:59 2024 - Thread Thread-4 (fetch_website) started.

Waiting for all threads...
Sun Dec  1 12:41:02 2024 - Fetched https://www.github.com
Sun Dec  1 12:41:04 2024 - Fetched https://www.example.com
Sun Dec  1 12:41:04 2024 - Thread Thread-1 (fetch_website) completed.
Waiting for all threads...
Sun Dec  1 12:41:09 2024 - Fetched https://www.wikipedia.org
Sun Dec  1 12:41:19 2024 - Fetched https://www.python.org
Sun Dec  1 12:41:19 2024 - Thread Thread-2 (fetch_website) completed.
Waiting for all threads...
Sun Dec  1 12:41:19 2024 - Thread Thread-3 (fetch_website) completed.
Waiting for all threads...
Sun Dec  1 12:41:19 2024 - Thread Thread-4 (fetch_website) completed.
Sun Dec  1 12:41:19 2024 - All websites fetched.
Total time taken: 20.00 seconds

进程已结束,退出代码为 0

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值