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