I am trying to understand multithreading in python, but every examples I read are very simple and similar to each other and don't serve my purpose.
I am performing high mathematics computation tasks so I need to use python mulithreading. I need to use multithreading NOT multiprocessing to avoid memory issues.
Below you will find a simple math problem that I wanna perform multithreading (Not an actual one, but let me understand threading). So i need to run task#1 (square) and task#2 (inve) in-parallel at the same time at two different threads (wait the results from the two), and then use the results from the two to obtain final result back in the main thread.
Please use any threading options in python (Thread, ThreadPool, ThreadPoolExecutor) anything you prefer, and really save time and cost comparing to serial code
If any one has better idea to make this code run faster, please share it
import scipy
import numpy
def square(A):
# take the square root of a matix
y = scipy.linalg.sqrtm(A)
return y
def inve(A):
# take the inverse of a matrix
y = numpy.linalg.inv(A)
def main(A):
# A is a matrix
# a is the square root of a matrix
# b is the inverse of a matix
# Run the first task on one thread
a = square(A)
# Run the second task in-parallel in another thread
b = inve(A)
# and then run final result in main thread
result = a * b
return result
A=[[1,2],[3,4]]
result = main(A)