打印运行线程名称| Concurrent.futures | Python 3

2024-09-30 01:28:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建此函数是为了从https://openlibrary.org/api/books获取书籍详细信息

def get_book_info(isbn):
    """Returns title, author and cover image URL from
    Open Library given an ISBN.

    Parameters:
    isbn (string): ISBN to search.

    Returns:
    tuple: tuple in the form (title (string), authors (list),
            cover image url (string))
    """

    openLibraryUrl = "https://openlibrary.org/api/books"
    parameters = {"bibkeys": f"ISBN:{isbn}", "format": "json", "jscmd": "data"}
    response = requests.get(openLibraryUrl, params=parameters)
    book_info = response.json()[f"ISBN:{isbn}"]

    title = book_info["title"]
    authorNames = []
    for author in book_info["authors"]:
        authorNames.append(author["name"])
    coverImageUrl = book_info["cover"]["small"] if "cover" in book_info else ""

    return (title, authorNames, coverImageUrl)

我正在用线程运行它并计算执行时间:

print ('Starting with 1 thread:')

threaded_start = time.time()
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
    futures = []
    for isbn in isbns_to_test:
        futures.append(executor.submit(get_book_info, isbn))
    for future in concurrent.futures.as_completed(futures):
        print (future.result())

print ('Threaded time:', time.time() - threaded_start)

在这种情况下,有人能帮助我理解如何获取concurrent.futures线程的名称吗?


Tags: ininfoforgetstringtimetitlecover

热门问题