Python Forum
Help with learning threading please. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help with learning threading please. (/thread-12999.html)



Help with learning threading please. - jarrod0987 - Sep-23-2018

I wrote a short test program to learn how to make threads. Right now there is only 1 thread. Going to increase that once I get it working.
It is throwing and error and I am not sure what I am doing wrong. Please advise. Thanks.

#Threading Test2 v1.0
from random import randint
import time, threading


def main():
    times = []
    for x in range(10):
        targetVar = 0
        start = time.time()
        t = threading.Thread(target = varUpdater, name = 'Thread1', args = (targetVar))
        t.start()
        
        end = time.time()
        times.append(end - start)
    average = (sum(times)/ len(times))
    print (average)
    

def varUpdater(targetVar):
    var = randint(0, 100)
    time.sleep(0.01)
    if var > targetVar:
        return var
    else:
        return targetVar

if __name__ == '__main__':
    main()
Error:
Exception in thread Thread1: Traceback (most recent call last): File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) TypeError: varUpdater() argument after * must be an iterable, not int Exception in thread Thread1: Traceback (most recent call last): File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) TypeError: varUpdater() argument after * must be an iterable, not int Exception in thread Thread1: Traceback (most recent call last): File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) TypeError: varUpdater() argument after * must be an iterable, not int Exception in thread Thread1: Traceback (most recent call last): File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) TypeError: varUpdater() argument after * must be an iterable, not int Exception in thread Thread1: Traceback (most recent call last): File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) TypeError: varUpdater() argument after * must be an iterable, not int Exception in thread Thread1: Traceback (most recent call last): File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) TypeError: varUpdater() argument after * must be an iterable, not int 0.0006911277770996094Exception in thread Thread1: Traceback (most recent call last): File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) TypeError: varUpdater() argument after * must be an iterable, not int Exception in thread Thread1: Traceback (most recent call last): File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) TypeError: varUpdater() argument after * must be an iterable, not int Exception in thread Thread1: Traceback (most recent call last): File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) TypeError: varUpdater() argument after * must be an iterable, not int Exception in thread Thread1: Traceback (most recent call last): File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) TypeError: varUpdater() argument after * must be an iterable, not int



RE: Help with learning threading please. - Gribouillis - Sep-23-2018

At line 11, I think args needs to be a tuple, so args = (targetVar,)


RE: Help with learning threading please. - stullis - Sep-23-2018

Looking at the traceback and error, something in your code needs to be an iterable (list, tuple, dict, or set) and is instead an integer. So, let's look for an integer being passed in. On line 11, you have:

t = threading.Thread(target = varUpdater, name = 'Thread1', args = (targetVar))
In order for the interpreter to read a single length tuple as a tuple, you need a comma after the item. Otherwise, the interpreter reads it as the type of the content rather than the iterable sequence (i.e. (tagetVar) == targetVar). To make this work, do this:

t = threading.Thread(target = varUpdater, name = 'Thread1', args = (targetVar,))
With the comma added, the interpreter now understands that this is a tuple.


RE: Help with learning threading please. - deanhystad - Sep-02-2020

from random import randint
import time, threading
 
def main():
    times = []
    for x in range(10):  # Thought you said only 1 thread, not 10!
        targetVar = 0
        start = time.time()
        t = threading.Thread(target = varUpdater, name = 'Thread1', args = (targetVar)) # args must be tuple
        t.start()
         
        end = time.time()  # Not measuring time for thread, only thread launch time
        times.append(end - start)
    average = (sum(times)/ len(times))
    print (average)
     
 
def varUpdater(targetVar):
    var = randint(0, 100)
    time.sleep(0.01)
    if var > targetVar:
        return var   # Why return value.  No way to use it.
    else:
        return targetVar
 
if __name__ == '__main__':
    main()
As others have said, the main problem is args has to be a tuple and tuples with only 1 element need a trailing comma or the parenthesis are treated as blocking, not tuple initialization.

Your start and stop times are not measuring how long the tread takes. For example:
import threading
import time
 
start_time = time.time()

def waiter(seconds):
    time.sleep(seconds)
    print ('waiter', time.time() - start_time)
    

def main():
    t = threading.Thread(target = waiter, name = 'Thread1', args = (5.0,))
    t.start()
    print ('main', time.time() - start_time)

main()
Output:
main 0.0 >>> waiter 5.007939100265503
It takes nearly zero time to launch the tread, but the tread takes 5 seconds to complete.