Reza
Reza

Reputation: 728

Why the threading part of this code does not run

In the code below, I get two matrices from user and try to calculate the multiplication of two matrices with thread. I'm new with thread and i don't know why the thread part of the code does not run?

My question is where is the problem of the code in threading part?

import threading
from queue import Queue
import time

import numpy as np

# get the number of rows and columns
r1, c1 = int(input("Enter the number of rows in matrix 1:   ")), int(input("Enter the number of columns in matrix 1:    "))

#initialize the matrix 1 with 0
matrix1 = np.zeros((r1,c1))

# get the matrix elements from user
for i in range(r1):
    for j in range(c1):
        matrix1[i][j] = int(input('enter element matrix[{}][{}]:    '.format(i, j)))

# get the number of rows and columns
r2, c2 = int(input("Enter the number of rows in matrix 2:  ")) , int(input("Enter the number of columns in matrix 2:   "))

#initialize the matrix 2 with 0
matrix2 = np.zeros((r2, c2))

# get the matrix elements from user
for i in range(r2):
    for j in range(c2):
        matrix2[i][j] = int(input('enter element matrix[{}][{}]:    '.format(i, j)))

print("matrix 1", matrix1)
print("matrix 2", matrix2)

# initializing the result matrix with 0
res_matrix = np.zeros((r1,c2))

q = Queue()

# multiply matrices using thread
def mat_mult(t):
    i = t[0]
    j = t[1]
    for a in range(c1):
        res_matrix[i][j] += matrix1[i][a] * matrix2[a][j]

    print(res_matrix)


def threader():
    while True:
        worker = q.get()
        #this print is just for checking if the threader function running 
        print("******",worker)
        mat_mult(worker)
        q.task_done()

# Create threads in number of elements of result matrix
for worker in range(r1 * c2):
    t = threading.Thread(target= threader())
    t.daemon = True
    t.start()

start = time.time()

for i in range(r1):
    for j in range(c2):
        t= (i,j)
        q.put((t))

q.join()

print(time.time()-start)

When i run the code and give two matrices to program, the program is still running with no output, but when i terminate it manually i got this:

Enter the number of rows in matrix 1:   2
Enter the number of columns in matrix 1:    3
enter element matrix[0][0]:    1
enter element matrix[0][1]:    2
enter element matrix[0][2]:    3
enter element matrix[1][0]:    4
enter element matrix[1][1]:    5
enter element matrix[1][2]:    6
Enter the number of rows in matrix 2:  3
Enter the number of columns in matrix 2:   2
enter element matrix[0][0]:    7
enter element matrix[0][1]:    8
enter element matrix[1][0]:    9
enter element matrix[1][1]:    10
enter element matrix[2][0]:    11
enter element matrix[2][1]:    12
matrix 1 [[ 1.  2.  3.]
 [ 4.  5.  6.]]
matrix 2 [[  7.   8.]
 [  9.  10.]
 [ 11.  12.]]
Traceback (most recent call last):
  File "/Users/saman/PycharmProjects/Syslab/test.py", line 46, in <module>
    t = threading.Thread(target= threader())
  File "/Users/saman/PycharmProjects/Syslab/test.py", line 40, in threader
    worker = q.get()
  File "/Applications/anaconda/lib/python3.5/queue.py", line 164, in get
    self.not_empty.wait()
  File "/Applications/anaconda/lib/python3.5/threading.py", line 293, in wait
    waiter.acquire()
KeyboardInterrupt

Upvotes: 0

Views: 66

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177600

Pass the target function correctly. You are currently calling the threader function instead of passing the function object. Remove the parentheses:

t = threading.Thread(target=threader)

By calling it, q.get() is called in the main thread and it hangs waiting for something to appear in the queue.

Upvotes: 4

Related Questions