在使用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()