Skip to content

《threading --- 基于线程的并行》中文文档示例代码格式化问题 #396

@LeoBenChoi

Description

@LeoBenChoi

《threading --- 基于线程的并行》中文文档示例代码格式化问题

《threading --- 基于线程的并行》这个文档的示例代码,没有进行格式化

页面链接:https://docs.python.org/zh-cn/3.14/library/threading.html

页面的示例代码是这样的:

import threading
import time

def crawl(link, delay=3):
print(f"crawl started for {link}")
time.sleep(delay) # 阻塞 I/O (模拟网络请求)
print(f"crawl ended for {link}")

links = [
"https://python.org",
"https://docs.python.org",
"https://peps.python.org",
]

# 针对每个链接启动线程
threads = []
for link in links:
# 使用 `args` 传入位置参数并使用 `kwargs` 传入关键字参数
t = threading.Thread(target=crawl, args=(link,), kwargs={"delay": 2})
threads.append(t)

# 启动每个线程
for t in threads:
t.start()

# 等待所有线程结束
for t in threads:
t.join()

应该格式化为

import threading
import time

def crawl(link, delay=3):
    print(f"crawl started for {link}")
    time.sleep(delay)  # Blocking I/O (simulating a network request)
    print(f"crawl ended for {link}")

links = [
    "https://python.org",
    "https://docs.python.org",
    "https://peps.python.org",
]

# Start threads for each link
threads = []
for link in links:
    # Using `args` to pass positional arguments and `kwargs` for keyword arguments
    t = threading.Thread(target=crawl, args=(link,), kwargs={"delay": 2})
    threads.append(t)

# Start each thread
for t in threads:
    t.start()

# Wait for all threads to finish
for t in threads:
    t.join()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions