我真的是线程的新手,这让我很困惑,我怎样才能并行运行这段代码?
def search_posts(page):
page_url = f'https://jsonplaceholder.typicode.com/posts/{page}'
req = requests.get(page_url)
res = req.json()
title = res['title']
return title
page = 1
while True:
with ThreadPoolExecutor() as executer:
t = executer.submit(search_posts, page)
title = t.result()
print(title)
if page == 20:
break
page += 1
另一个问题是我是否需要学习操作系统才能理解线程是如何工作的?
PIPIONE
相关分类