Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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线程,作为10个位置参数的10个链接列表_Python_Multithreading - Fatal编程技术网

Python线程,作为10个位置参数的10个链接列表

Python线程,作为10个位置参数的10个链接列表,python,multithreading,Python,Multithreading,有一只蜘蛛正在尝试爬网并添加到数据库中,我想我会使用线程来加快速度 代码如下: def final_function(link_set): root = 'http://www.rightmove.co.uk' pages = [] for link in link_set: try: links = forty_page_getter(link) pages.append(links)

有一只蜘蛛正在尝试爬网并添加到数据库中,我想我会使用线程来加快速度

代码如下:

def final_function(link_set):
    root = 'http://www.rightmove.co.uk'
    pages = []

    for link in link_set:


        try:
            links = forty_page_getter(link)
            pages.append(links)
        except:
            print('not possible for:' + str(link))
            pass
    flattened = [item for sublist in pages for item in sublist]
    print('flattened done')

    for page in flattened:
        print(len(flattened))
        try:
            page_stripper(link=(root+page))
        except:
            print('couldnt do it for')
            pass
这是最后一个函数,它接受链接列表作为参数。 我的问题是:

if __name__ == "__main__":

    areas = pd.read_csv('postcodes.csv')
    areas = areas['0']
    result_list = split_list(flattened=areas, chunk_size=10)
    threads = []
    outer_count = 1


    # here ten postcode links
    for i in result_list:
        print('Started thread No. ' + str(outer_count))
        t = threading.Thread(target=final_function, args=i)
        threads.append(t)
        t.start()
        outer_count += 1
我是链接的子列表,从中我可以得到房屋数据,它的长度是10,这就是为什么我得到一个例外

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
TypeError: final_function() takes 1 positional argument but 10 were given
还有什么我可以跳过的吗?我坚持自己的想法,因为我认为简单地把它作为一个论点来传递是有意义的

编辑:我自己解决了,我不知道为什么,但你需要做的就是

t = threading.Thread(target=final_function, args=(i,)) 

它解决了
线程中的

args
问题。线程
应该是一个参数元组,这意味着当您将iterable(list)传递给它时,它会将每个列表元素视为单独的参数

可以通过将包含列表的元组传递给
args
,如

for i in result_list:
    t = threading.Thread(target=final_function, args=(i,))

threading.Thread
中的
args
应该是一个参数元组,这意味着当您将iterable(list)传递给它时,它会将每个列表元素视为单独的参数

可以通过将包含列表的元组传递给
args
,如

for i in result_list:
    t = threading.Thread(target=final_function, args=(i,))