【发布时间】:2020-03-29 13:54:55
【问题描述】:
我需要多次生成一个pyx文件并在一个运行时运行它的重新编译并将相应的扩展重新加载到程序中。 这是一个简化的例子:
from setuptools import Extension, setup
from Cython.Build import cythonize
import sys
pyxfile = "foo.pyx"
def write_pyx(incval):
with open(pyxfile, 'w') as f:
f.write('cpdef int foo(int x):\n return x+%i' % incval)
def ext_compile():
oldargv = sys.argv
sys.argv = ['thisfile.py', 'build_ext', '--inplace']
setup(
ext_modules=cythonize(
[ Extension("example", [pyxfile]) ],
compiler_directives={'language_level': 2}
)
)
sys.argv = oldargv
write_pyx(1)
ext_compile()
import example
print "foo(1) =", example.foo(1)
write_pyx(10)
ext_compile()
reload(example)
print "foo(1) =", example.foo(1)
然而,在执行时,尽管 pyx 文件发生了变化,但我只有一个编译。这是控制台的输出:
Compiling foo.pyx because it changed.
[1/1] Cythonizing foo.pyx
running build_ext
building 'example' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/artem/.pyenv/versions/2.7.16/include/python2.7 -c foo.c -o build/temp.linux-x86_64-2.7/foo.o
gcc -pthread -shared -L/home/artem/.pyenv/versions/2.7.16/lib build/temp.linux-x86_64-2.7/foo.o -o build/lib.linux-x86_64-2.7/example.so
copying build/lib.linux-x86_64-2.7/example.so ->
foo(1) = 2
running build_ext
copying build/lib.linux-x86_64-2.7/example.so ->
foo(1) = 2
知道如何解决这个问题吗?
【问题讨论】:
-
即使它可以编译,重新加载仍然无法按您的预期工作,例如:stackoverflow.com/a/55172547/5769463