Python Forum
Help with learning threading please.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with learning threading please.
#1
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
Reply
#2
At line 11, I think args needs to be a tuple, so args = (targetVar,)
Reply
#3
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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,844 May-03-2023, 08:22 AM
Last Post: billykid999
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,127 Oct-01-2021, 08:32 PM
Last Post: muzikman
  [split] Help with learning threading please. kalinuxman 1 1,832 Sep-02-2020, 04:40 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020