1

I am right now making a program that ask user to input the determinant of a matrix. at a first, a very complicate matrix will be generated, after two seconds, a new matrix will be generated.

Here is my code

#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()

It works will until the timer part the "Please Tell me the dot product of this Matrix" will alert at the same time with the first matrix, and two seconds later. the console says

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

I would appreciate a lot if someone can help me with this easy problem

J.Zhou
  • 27
  • 1
  • 7

3 Answers3

1

You need to pass a callable object so use a lambda:

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

Or pass random_matrix_integer using args to pass n :

t = threading.Timer(2,random_matrix_integer, args=(3,))
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

UPDATED:

The return value of any multiprocessing method will be None.

So you can do the following:

#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()

Output:

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]]

Alternatively, if you intend to share or handle the return variable later, you can create and use multiprocessing.Array or multiprocessing.Manager variables which can store the values you've got, thus you can manipulate such a "return" value after multiprocessing.

Geeocode
  • 5,705
  • 3
  • 20
  • 34
0

You need to pass a function to Timer, not the result of a call to a function.

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

Notice the lack of parentheses - this means you pass foo, not the result of foo()

deets
  • 6,285
  • 29
  • 28