Python2计时器不遵循顺序
我现在正在制作一个程序,要求用户输入矩阵的行列式。 首先,将生成一个非常复杂的矩阵,两秒钟后,将生成一个新矩阵 这是我的密码Python2计时器不遵循顺序,python,multithreading,python-2.7,numpy,timer,Python,Multithreading,Python 2.7,Numpy,Timer,我现在正在制作一个程序,要求用户输入矩阵的行列式。 首先,将生成一个非常复杂的矩阵,两秒钟后,将生成一个新矩阵 这是我的密码 #import part import numpy import scipy import threading from multiprocessing import pool #defining part def random_matrix(n): array = numpy.random.random((n,n)) print array def r
#import part
import numpy
import scipy
import threading
from multiprocessing import pool
#defining part
def random_matrix(n):
array = numpy.random.random((n,n))
print array
def random_matrix_integer(n):
print "Sorry I was just joking, Please calculate this one."
array = numpy.random.random_integers(0,10,(n,n))
print array
#process part
print "Now,let's start a mathematical game."
random_matrix(3)
print "Please Tell me the dot product of this Matrix\n"
t =threading.Timer(2,random_matrix_integer(3))
t.start()
它将一直工作到计时器部分
“请告诉我此矩阵的点积”将与第一个矩阵同时发出警报,
两秒钟后。控制台说
Exception in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 1082, in run
self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable
如果有人能帮我解决这个简单的问题,我将不胜感激
Now,let's start a mathematical game.
[[ 0.00496393 0.64893226 0.72005749]
[ 0.75589265 0.2527754 0.61459672]
[ 0.89208782 0.54560049 0.57733855]]
Please Tell me the dot product of this Matrix
Sorry, I was just joking, let's calculate the result of this one.
[[10 2 5]
[ 9 1 0]
[ 4 6 6]]
任何多处理
方法的返回值都将是None
因此,您可以执行以下操作:
#defining part
def random_matrix(n):
array = numpy.random.random((n,n))
print array
def random_matrix_integer(n):
output = numpy.random.random_integers(0,10,(n,n))
print output
def next(n):
print "Sorry, I was just joking, let's calculate the result of this one."
random_matrix_integer(n)
#process part
print "Now,let's start a mathematical game."
random_matrix(3)
print "Please Tell me the dot product of this Matrix\n"
t =threading.Timer(2,next,args=(3,))
t.start()
输出:
Now,let's start a mathematical game.
[[ 0.00496393 0.64893226 0.72005749]
[ 0.75589265 0.2527754 0.61459672]
[ 0.89208782 0.54560049 0.57733855]]
Please Tell me the dot product of this Matrix
Sorry, I was just joking, let's calculate the result of this one.
[[10 2 5]
[ 9 1 0]
[ 4 6 6]]
或者,如果您打算稍后共享或处理返回变量,您可以创建并使用
多处理.Array
或多处理.Manager
变量,这些变量可以存储您获得的值,因此您可以在多处理后操作这样的“返回”值。您需要将函数传递给计时器,不是函数调用的结果
import threading
def foo():
print "foo"
t =threading.Timer(2, foo)
t.start()
请注意缺少括号-这意味着您传递的是
foo
,而不是foo()
的结果。您需要传递一个可调用对象,因此使用lambda
:
t =threading.Timer(2,lambda: random_matrix_integer(3))
或者使用args传递随机矩阵\u整数
,以传递n
:
t = threading.Timer(2,random_matrix_integer, args=(3,))
阴影可能是个坏主意。。。另外,您是否意识到您正在调用它,并将结果(
None
)传递给Timer
?是的,结果为None,为什么会发生这种情况,我如何解决此问题?请参阅@J.Zhou查看我的更新答案非常感谢,我的问题已解决