为什么此代码的线程部分不运行

时间:2016-12-25 00:21:11

标签: python multithreading python-multithreading

在下面的代码中,我从用户那里得到两个矩阵,并尝试用thread计算两个矩阵的乘法。我是thread的新用户,我不知道为什么代码的thread部分无法运行?

我的问题是线程部分代码的问题在哪里?

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)

当我运行代码并给出两个矩阵进行编程时,程序仍在运行时没有输出,但是当我手动终止它时我得到了这个:

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

1 个答案:

答案 0 :(得分:4)

正确传递目标功能。您当前正在调用threader函数,而不是传递函数对象。删除括号:

t = threading.Thread(target=threader)

通过调用它,在主线程中调用q.get()并挂起等待队列中出现的内容。

相关问题