Forcing modules to run in PyPy from a CPython script (run PyPy on part of the code)?

£可爱£侵袭症+ 提交于 2019-12-11 10:48:10

问题


Is there a way to import modules from a CPython script, but run them in PyPy?

The problem is that I have a code that uses lots of SciPy (and NumPy), but there are parts of the code that could be optimized with PyPy.

Here's a random example of what I would like to do:

sub_run_in_pypy.py module:

#assuming this can be optimized with PyPy
def function_a(foo):
    return foo**2

main_run_in_cpython.py module:

import scipy.stats as stats

#assuming this exists:
import import_function_for_pypy
pypy_imported_function = import_function_for_pypy(module_name=sub_run_in_pypy, function_name=function_a)

x = stats.t.rvs(5, loc=0, scale=1, size=1)

print pypy_imported_function(x)

If this does not exist, why not?

Edit: As Bakuriu inferred, I was suggesting it could potentially be something that runs in a separate process. Would this add too much overhead?


回答1:


Since I stumbled over this question long before the other thread, the reverse mechanism is described briefly here (Can I embed CPython inside PyPy?).

The basic idea is to start a PyPy interpreter alongside the CPython interpreter (or vise versa) and connect them via inter process communication. While you might be tempted to try to do this via pipes or sockets, it is highly recommended to use a higher-level library, such as execnet (which is actually used for this purpose).

If you go for a low-level approach, be sure to decide early if and how you can handle architectures such as multi-thread or multi-process execution, whether you want asynchronous computation or even master-worker setups.



来源:https://stackoverflow.com/questions/15878223/forcing-modules-to-run-in-pypy-from-a-cpython-script-run-pypy-on-part-of-the-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!