强制模块从CPython脚本在PyPy中运行(在部分代码上运行PyPy)? [英] Forcing modules to run in PyPy from a CPython script (run PyPy on part of the code)?

查看:158
本文介绍了强制模块从CPython脚本在PyPy中运行(在部分代码上运行PyPy)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从CPython脚本中导入模块,但是可以在PyPy中运行它们?

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

问题是我有一个使用大量SciPy的代码(和NumPy),但是有部分代码可以使用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模块:

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模块:

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?

编辑:正如巴库里(Bakuriu)推断的那样,我建议它可能是在单独的流程中运行的东西。这会增加过多的开销吗?

推荐答案

由于我在另一个线程很久以前就无意中发现了这个问题,反之在此(我可以将CPython嵌入PyPy内吗?)

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

基本思想是与CPython解释器一起启动PyPy解释器(反之亦然),并通过进程间通信将它们连接起来。尽管您可能会尝试通过管道或套接字尝试执行此操作,但强烈建议您使用更高级别的库,例如 execnet (实际上是用于此目的)。

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.

这篇关于强制模块从CPython脚本在PyPy中运行(在部分代码上运行PyPy)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆