Calling pypy functions from C-python

别等时光非礼了梦想. 提交于 2021-02-08 06:12:17

问题


I want to plot CPU intensive functions with matplotlib. I would like to use pypy but it is not compatible with matplotlib. The plotting itself is not CPU intensive and does not need to be accelerated. I wonder if there is a way to call a pypy function from C-python. Could I (ab)use the multiprocessing module and say set_executable("/.../pypy") from C-python?


回答1:


You could write the output of your computation to stdout and pipe it to another program that reads the data to plot from stdin, like so:

pypy compute.py | python plot.py 

As the intermediary format, you could use a format like JSON which are available in the standard library of both pypy and cpython and can convert from and to python primitives easily.

Alternatively, the compute.py could just use the subprocess module to start plot.py in CPython instead of relying on the pipe being arranged by the shell.

Alternatively, you could use pickle which can preserve more information about python objects, but be careful since pickle isn't a well standardized format.

If you use a recent enough version of CPython and Pypy, another alternative you can try is the multiprocessing.connection module's Listener and Client classes. Note that cross python implementation Listener and Client was broken in some older version of Python and Pypy (multiprocessing Listeners and Clients between python and pypy). The multiprocessing.manager module probably would also work across different cpython and pypy.




回答2:


If you have CPU intensive functions the best bet is to:

A) See if numpy can help,

B) See if the functions can be optimised in python,

C) Consider writing C extension for your function.

D) Consider embedding cpython and matplotlib in pypy as demonstrated here.




回答3:


You may use execnet. There are several exmpales of Connecting different Python interpreters



来源:https://stackoverflow.com/questions/20756732/calling-pypy-functions-from-c-python

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