考虑这个 fortran 模块 (example.f90)
module mod
implicit none
contains
subroutine test(a,b)
double precision, intent(in) :: a
double precision, intent(out) :: b
b = 2.d0*a
end subroutine
end module
我可以用 f2py
和命令 python -m numpy.f2py -c example.f90 -m example
将它包装起来。
以下 Python
代码不起作用。它产生错误 TypeError: can't pickle fortran objects
from pathos.multiprocessing import ProcessingPool as Pool
from example import mod as mod1
def function(inpt):
mod = mod1
def test(x):
return mod.test(x)
with Pool(5) as p:
result = p.map(test, inpt)
return result
inpt = [1.,2.,3.,4.,5.]
result = function(inpt)
print(result)
尽管如此,当我将 Python 代码稍微更改为以下内容时,它运行时没有错误:
from pathos.multiprocessing import ProcessingPool as Pool
from example import mod
def function(inpt):
def test(x):
return mod.test(x)
with Pool(5) as p:
result = p.map(test, inpt)
return result
inpt = [1.,2.,3.,4.,5.]
result = function(inpt)
print(result)
关于在 mod
中重新定义 Fortran 模块对象 function
的事情,把一切都搞砸了。
我正在寻找一种变通方法,它允许我在 function
中重新定义 fortran 模块(即上面的第一个 python 示例)。
谢谢