编辑:这是一个同情错误。我已将讨论移至https://github.com/sympy/sympy/issues/7457
我有一个 Python 程序,用于sympy
执行一些核心功能,这些功能涉及将线和形状相交。此操作需要执行数千次,并且在使用默认的sympy
纯 Python 模块时非常慢。
我试图通过安装来加快速度gmpy 2.0.3
(我也尝试过gmpy 1.5
)。这确实会导致代码在一定程度上加速,但是当使用multiprocessing
它来获得进一步的加速时,程序会以TypeError
.
Exception in thread Thread-3:
Traceback (most recent call last):
File "C:\python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\python27\lib\threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "C:\python27\lib\multiprocessing\pool.py", line 376, in _handle_results
task = get()
File "C:\python27\lib\site-packages\sympy\geometry\point.py", line 91, in __new__
for f in coords.atoms(Float)]))
File "C:\python27\lib\site-packages\sympy\simplify\simplify.py", line 3839, in nsimplify
return _real_to_rational(expr, tolerance)
File "C:\python27\lib\site-packages\sympy\simplify\simplify.py", line 3781, in _real_to_rational
r = nsimplify(float, rational=False)
File "C:\python27\lib\site-packages\sympy\simplify\simplify.py", line 3861, in nsimplify
exprval = expr.evalf(prec, chop=True)
File "C:\python27\lib\site-packages\sympy\core\evalf.py", line 1300, in evalf
re = C.Float._new(re, p)
File "C:\python27\lib\site-packages\sympy\core\numbers.py", line 673, in _new
obj._mpf_ = mpf_norm(_mpf_, _prec)
File "C:\python27\lib\site-packages\sympy\core\numbers.py", line 56, in mpf_norm
rv = mpf_normalize(sign, man, expt, bc, prec, rnd)
TypeError: ('argument is not an mpz', <class 'sympy.geometry.point.Point'>, (-7.07106781186548, -7.07106781186548))
该程序在使用单个进程gmpy
运行和不gmpy
使用运行时运行良好multiprocessing.Pool
。
有没有人遇到过这种问题?下面的程序重现了这个问题:
import sympy
import multiprocessing
import numpy
def thread_function(func, data, output_progress=True, extra_kwargs=None, num_procs=None):
if extra_kwargs:
func = functools.partial(func, **extra_kwargs)
if not num_procs:
num_procs = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=num_procs)
results = pool.map_async(func, data.T)
pool.close()
pool.join()
return results.get()
def test_fn(data):
x = data[0]
y = data[1]
circle = sympy.Circle((0,0), 10)
line = sympy.Line(sympy.Point(0,0), sympy.Point(x,y))
return line.intersection(circle)[0].evalf()
if __name__ == '__main__':
data = numpy.vstack((numpy.arange(1, 100), numpy.arange(1, 100)))
print thread_function(test_fn, data) #<--- this line causes the problem
# print [test_fn(data[:,i]) for i in xrange(data.shape[1])] #<--- this one runs without errors