multithreading - python mutithread and strangeTypeError: takes exactly 1 argument (2 given) -


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

above code, , when args 1 9, works well, when comes 10 , larger comes error:

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 1 argument (2 given) 

i not figure out it's wrong.

thread gets args , use function way:

crawler(args[0], args[1], ...) 

when str(i+1) has 1 char gives

crawler(args[0]) 

when str(i+1) has 2 chars gives

crawler(args[0], args[1]) 

but functions expects 1 argument.

you have use list or tuple in thread

thread(target = crawler,args = [ str(i+1) ] )  thread(target = crawler,args = ( str(i+1), ) ) 

and args[0] str(i+1) 1 element.


Comments