为什么这段代码的线程部分没有 运行
Why the threading part of this code does not run
在下面的代码中,我从用户那里得到了两个矩阵,并尝试计算两个矩阵与 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
正确传递目标函数。您当前正在调用 threader
函数而不是传递函数对象。删除括号:
t = threading.Thread(target=threader)
通过调用它,q.get()
在主线程中被调用,它挂起等待队列中出现的东西。
在下面的代码中,我从用户那里得到了两个矩阵,并尝试计算两个矩阵与 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
正确传递目标函数。您当前正在调用 threader
函数而不是传递函数对象。删除括号:
t = threading.Thread(target=threader)
通过调用它,q.get()
在主线程中被调用,它挂起等待队列中出现的东西。