Python Threading問題:TypeError in Threading. function takes 1 positional argument but 100 were given


在使用python多線程module Threading時:

import threading
t = threading.Thread(target=getTemperature, args = (id1))
t.start()

運行時報如上的錯誤,參考stackoverflow,如下解釋:

The args kwarg of threading.Thread expects an iterable, and each element in that iterable is being passed to the target function.
Since you are providing a string for args: 
  t = threading.Thread(target=startSuggestworker, args = (start_keyword)) 
each character is being passed as a separate argument to startSuggestworker. 
Instead, you should provide args a tuple: 
  t = threading.Thread(target=startSuggestworker, args = (start_keyword,)) 
也就是args傳遞的參數類型不對,即使一個參數也要時元組的形式給出

正確的傳遞方式如下:

import threading
t = threading.Thread(target=getTemperature, args = (id1,))
t.start()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM