0
我剛開始用多線程,所以我想我會做我自己一個小而簡單的例子:Python的多線程參數錯誤
import time
import threading
def count(who):
count = 1
while count <= 5:
print who + " counted to: " + str(count)
time.sleep(0.1)
count += 1
thread1 = threading.Thread(target=count, args=('i'))
thread1.start()
偉大的工程,並打印出以下幾點:
>>> i counted to: 1
>>> i counted to: 2
>>> i counted to: 3
>>> i counted to: 4
>>> i counted to: 5
奇怪的是但是,當我想參數更改爲另一個名字: 「約翰」:
線程1 = threading.Thread(目標=算,ARGS =( '約翰'))
希望它會產生:
>>> john counted to: 1
>>> john counted to: 2
>>> john counted to: 3
>>> john counted to: 4
>>> john counted to: 5
然而,procudes錯誤:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
TypeError: count() takes exactly 1 argument (4 given)
我真的不知道這裏發生了什麼?有誰知道?
哦,好的,嗯,我想這是有道理的。謝謝你的解釋! – user5740843