Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork85
Open
Description
《threading --- 基于线程的并行》中文文档示例代码格式化问题
《threading --- 基于线程的并行》这个文档的示例代码,没有进行格式化
页面链接:https://docs.python.org/zh-cn/3.14/library/threading.html
页面的示例代码是这样的:
importthreadingimporttimedefcrawl(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= []forlinkinlinks:# 使用 `args` 传入位置参数并使用 `kwargs` 传入关键字参数t=threading.Thread(target=crawl,args=(link,),kwargs={"delay":2})threads.append(t)# 启动每个线程fortinthreads:t.start()# 等待所有线程结束fortinthreads:t.join()
应该格式化为
importthreadingimporttimedefcrawl(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 linkthreads= []forlinkinlinks:# Using `args` to pass positional arguments and `kwargs` for keyword argumentst=threading.Thread(target=crawl,args=(link,),kwargs={"delay":2})threads.append(t)# Start each threadfortinthreads:t.start()# Wait for all threads to finishfortinthreads:t.join()
Metadata
Metadata
Assignees
Labels
No labels