Reputation: 103
I am new to Python threading, I have tried the below code to understand the threads:
import threading
def sq(num):
for n in num:
print("calculating square")
print('square={}'.format(n*n))
numbers=[10,20,30,40,50,60]
t1 = threading.Thread(target=sq, args=(numbers,))
t1.start()
t1.join()
As far as I understood, the above code issues a single thread to calculate the squares of numbers.
I wanted to know if it is possible to spawn n different threads to calculate squares of n numbers? For example, if I have five numbers in numbers array as [10,20,30,40,50]
. Instead of issuing one single thread is it possible to issue 5 concurrent threads where each thread calculates the sq of a number.
I have tried the below code for doing it, but I am not sure if it had issued n concurrent threads(where n=len(numbers)
). The basic idea is to issue n different threads concurrently, where each thread calculates the square of one element in the numbers array, the first thread should calculate sq of the first element in the numbers array similarly fifth thread should calculate sq of the fifth element in numbers array and so on.
The simplest idea is to add n threading. Thread statements and issue the threads but is there any other way to make it simpler:
import threading
def sq(num):
print("calculating square")
print('square={}'.format(num*num))
numbers=[10,20,30,40,50,60]
for i in range(0,len(numbers)):
t1 = threading.Thread(target=sq, args=(numbers[i],))
t1.start()
t1.join()
Upvotes: 1
Views: 1792
Reputation: 1427
You could utilise a thread pool.
import multiprocessing as mp
from multiprocessing.dummy import Pool as ThreadPool
def sq(num):
return num * num
with ThreadPool(mp.cpu_count()) as pool:
results = pool.map(sq, [10,20,30,40,50,60])
print(results)
multiprocessing.dummy
replicates the API for multi threading. The above code generates a series of threads equal to your CPU core count then calls sq
on each element in the array.
Documentation for multiprocessing.dummy
Edit. I just noticed how old this question is but I will keep my answer up.
Upvotes: 1
Reputation: 957
You should be able to. But your example would overwrite t1
in every iteration. Also don't join()
the thread before starting all of them.
Pseudo solution:
threads = []
for number in numbers:
new_thread = threading.Thread(target=sq, args=(number,))
threads.append(new_thread)
new_thread.start()
for old_thread in threads:
old_thread.join()
Upvotes: 0