1 | initial version |
The problem is that the GAP interface is not threadsafe -- both threads are accessing the same GAP session. You could use the @parallel
decorator and forking (which has a bit of overhead). For example,
def method_1(n):
import time
time.sleep(20)
return 1, Integer(gap(n)*n)
def method_2(n):
return 2, Integer(gap(n)*n)
def try_fast(n):
@parallel
def try_method(method):
return method(n)
method_num, result = try_method([method_1, method_2]).next()[1]
print "Computed by method %s"%method_num
return result
The result of the (decorated) try_method function is an iterator which will yield the results as soon as they are ready. In this case, we just need the first one.
sage: %time try_fast(4)
Killing any remaining workers...
Computed by method 2
CPU times: user 0.00 s, sys: 0.02 s, total: 0.02 s
Wall time: 0.27 s
16