python multithread和strangeTypeError:正好采用1个参数(给定2个)

时间:2016-01-29 03:41:27

标签: python multithreading python-2.7

def crawler(id):
    print id
    crawer.getCourseFromUrl("http://www.imooc.com/view/"+id)
    time.sleep(3)
def main():
    print '*** Starting crawler ***'
    try:
        for id in xrange(100):
            threads = []
            for i in range(10):
                t = threading.Thread(target = crawler,args = str(i+1))
                threads.append(t)
            for t in threads:
                t.start()
            for t in threads:
                t.join()
            for t in threads:
                t.close()
    except:
        pass
    print '*** crawler End ***'

以上是我的代码,当args为1到9时,它运行良好,但是当它达到10和更大时,它会出现错误:

Exception in thread Thread-10:
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
TypeError: crawler() takes exactly 1 argument (2 given)

我无法弄清楚它是什么错误。

2 个答案:

答案 0 :(得分:0)

尝试将args作为元组传递:

t = threading.Thread(target = crawler, args = (str(i+1),))

如此处https://docs.python.org/2/library/threading.html#threading.Thread

所述

答案 1 :(得分:0)

undefined获取Thread并以这种方式使用函数:

args

crawler(args[0], args[1], ...) 只有一个字符时,它会给出

str(i+1)

crawler(args[0]) 有两个字符时,它会给出

str(i+1)

但是你的函数只需要一个参数。

您必须在crawler(args[0], args[1])

中使用列表或元组
Thread

然后Thread(target = crawler,args = [ str(i+1) ] ) Thread(target = crawler,args = ( str(i+1), ) ) 会将所有args[0]作为一个元素。