1

So I was playing around with the threading module in order to understand the basics of it and when I ran my code, I get an error that does not clearly specify what did I do wrong. here is the code, and bellow that, you can find the error log:

import threading
import time

start = time.perf_counter()


def square(y):
    print('Starting processing')
    for x in range(y):
        i = x * x
    print(f'Done processing {i}')

threads = []

# creating thread
for _ in range(10):
    thread = threading.Thread(target=square, args=1000000)
    # starting thread
    thread.start()
    threads.append(thread)

# makes sure that the threads complete before moving to the rest of the code
for i in threads:
    i.join()




finish = time.perf_counter()

print(f'Done processing in {round(finish - start, 4)} second(s).')

And I get this error:

Exception in thread Thread-1:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in 
    runself._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-3:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, 
    in_bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in 
    runself._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-4:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_inner
self.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in 
    runself._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-2:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int
Exception in thread Thread-5:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run 
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int


Exception in thread Thread-6:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-7:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-8:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-9:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-10:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

I do not know what is wrong with my code, and the error log does not indicate where did the error stem from.

2
  • 2
    Your args= parameter needs to be iterable. I suggest a tuple: Thread(target=square, args=(1000000,)) Commented Jun 8, 2020 at 9:28
  • thank you, that was it. Commented Jun 8, 2020 at 9:33

1 Answer 1

15

Try

thread = threading.Thread(target=square, args=(1000000,))

The function is expecting an tuple / list argument, which is iterable, so if you have more arguments to pass, you just do it like this args=(param1, param2, param3).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that was it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.