2015-07-19 95 views
1

我现在正在制作一个程序,要求用户输入矩阵的行列式。首先会产生一个非常复杂的矩阵,在两秒钟后,会产生一个新的矩阵。python 2计时器不遵循命令

这里是我的代码

#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 

我将不胜感激很多,如果有人可以帮助我这个简单的问题

+0

Shadowing ['next'](https://docs.python.org/2/library/functions.html#next)可能是一个坏主意......另外,你是否意识到你正在*调用*它,并将结果('None')传递给'Timer'? – jonrsharpe

+0

是的结果是None,为什么会发生这种情况,我该如何解决这个问题? –

+0

查看http://stackoverflow.com/q/8269096/3001761 – jonrsharpe

回答

0

更新:

任何multiprocessing方法的返回值将是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]] 

另外,如果你想分享或处理返回变量以后,你可以创建和使用multiprocessing.Arraymultiprocessing.Manager变量可以存储您已获得的值,因此您可以在多处理后处理这样的“返回”值。

0

您需要将一个函数传递给Timer,而不是将一个函数调用的结果。

import threading 
def foo(): 
    print "foo" 
t =threading.Timer(2, foo) 
t.start() 

通知缺少括号的 - 这意味着你通过foofoo()

1

没有结果你需要传递一个可调用对象,以便使用lambda

t =threading.Timer(2,lambda: random_matrix_integer(3)) 

或者通过random_matrix_integer使用参数通过n

t = threading.Timer(2,random_matrix_integer, args=(3,))