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.
args=parameter needs to be iterable. I suggest atuple:Thread(target=square, args=(1000000,))