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


Messages In This Thread
Help with learning threading please. - by jarrod0987 - Sep-23-2018, 06:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,908 May-03-2023, 08:22 AM
Last Post: billykid999
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,146 Oct-01-2021, 08:32 PM
Last Post: muzikman
  [split] Help with learning threading please. kalinuxman 1 1,845 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