Reputation: 17
I am using threading to speed up my processes, but it says my thread is not callable. Code:
thread1 = threading.Thread(target=next_word())
thread2 = threading.Thread(target=get_word())
thread1.start()
thread2.start()
The error is this:
Exception in thread Thread-1: Traceback (most recent call last): File "C:\Users\Tom\AppData\Local\Programs\Python\Python35\lib\threading.py", line 914, in _bootstrap_inner self.run() File "C:\Users\Tom\AppData\Local\Programs\Python\Python35\lib\threading.py", line 862, in run self._target(*self._args, **self._kwargs) TypeError: 'str' object is not callable
I do not know what is wrong with my code, if you can help at all that will be great. I know there is two errors, but they are the same so by fixing one, the other can be fixed. Thank you in advance.
Upvotes: 1
Views: 744
Reputation: 5224
You are expected to give a function for Thread
as target.
While next_word
is a function, next_word()
is not. Instead, it is the result of the function, that is probably a string.
So what is happening is your Thread
will call next_word()()
, i.e "a string"()
which obviously doesn't make sense since a string can't be called, hence your TypeError: 'str' object is not callable
.
Fix :
thread1 = threading.Thread(target=next_word)
thread2 = threading.Thread(target=get_word)
thread1.start()
thread2.start()
Upvotes: 2