Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多线程中的异常-Python_Python_Multithreading - Fatal编程技术网

多线程中的异常-Python

多线程中的异常-Python,python,multithreading,Python,Multithreading,我正在尝试使用以下函数使用多线程: import requests 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 th

我正在尝试使用以下函数使用多线程:

import requests


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)
测试的输入是:

isbns_to_test = ["0134692888","1491946008","1491957662","0134853989","1449340377","1449355730",
                 "1491939362","1775093301","1593279280","1593279922",
                ]
我用于执行多线程处理的代码以及我需要更正的代码如下:


from threading import Thread

# initialize 'threads'
threads = []

for i, num in enumerate (isbns_to_test):
    thread = Thread(target = get_book_info, args =(i, num))
    threads.append(thread)
    thread.start()

# stopping 'threads'
for i, thread in enumerate (threads):
    thread.join()
 
我得到了一个错误,我不知道为什么会发生。我的目标是使用
%%time
测量时间

Exception in thread Thread-4:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: get_book_info() takes 1 positional argument but 2 were given
Exception in thread Thread-5:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: get_book_info() takes 1 positional argument but 2 were given
Exception in thread Thread-6:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
Exception in thread Thread-7:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
Exception in thread Thread-8    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: get_book_info() takes 1 positional argument but 2 were given
:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
        self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: get_book_info() takes 1 positional argument but 2 were given
Exception in thread Thread-9:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self._target(*self._args, **self._kwargs)
TypeError: get_book_info() takes 1 positional argument but 2 were given
Exception in thread Thread-10:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: get_book_info() takes 1 positional argument but 2 were given
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: get_book_info() takes 1 positional argument but 2 were given
Exception in thread Thread-11:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
Exception in thread Thread-12    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: get_book_info() takes 1 positional argument but 2 were given
:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
Exception in thread Thread-13:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: get_book_info() takes 1 positional argument but 2 were given
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: get_book_info() takes 1 positional argument but 2 were given
有人知道为什么会发生这种错误吗?
提前感谢。

该函数只有一个参数:

def get_book_info(isbn):
创建线程时,传递两个参数:

thread = Thread(target = get_book_info, args =(i, num))
传递一个参数:

thread = Thread(target = get_book_info, args =(num,))
或向函数签名添加一个参数:

def get_book_info(i,isbn):

您正在传递两个参数,
(i,num)
,以
获取图书信息
,它只接受一个参数,
isbn
;为什么您认为这不会产生错误?