https://github.com/swoole/phpy
Connecting the Python and PHP ecosystems together
https://github.com/swoole/phpy
Last synced: 7 months ago
JSON representation
Connecting the Python and PHP ecosystems together
- Host: GitHub
- URL: https://github.com/swoole/phpy
- Owner: swoole
- License: apache-2.0
- Created: 2023-12-04T09:15:17.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-06-16T06:44:39.000Z (8 months ago)
- Last Synced: 2025-06-16T07:25:12.193Z (8 months ago)
- Language: PHP
- Homepage:
- Size: 1.87 MB
- Stars: 602
- Watchers: 16
- Forks: 50
- Open Issues: 4
-
Metadata Files:
- Readme: README-CN.md
- License: LICENSE
Awesome Lists containing this project
- awesome-php-ml - swoole/phpy
README
[English](README.md)
phpy
====
`Python` 与 `PHP` 互调用库,可以在 `PHP` 中使用 `Python` 语言的函数和类库,或者在 `Python` 中使用 `PHP` 的包。
但不是语言内嵌。编码依然使用各自的原生语法。

- 查看 [中文文档](docs/cn/README.md)
- 支持 `Linux`/`Windows`/`macOS` 等多种操作和平台
- 不支持 Python 多线程、`async-io` 特性
- 需要 `PHP 8.1` 或更高版本
PHP 调用 Python
----
编译安装 `phpy.so` 作为扩展加载,修改 `php.ini` 追加 `extension=phpy.so` 即可。
例子:
```php
$os = PyCore::import("os");
$un = $os->uname();
echo strval($un);
```
Python 中调用 PHP
----
直接作为 `C++ Mudule` ,import 加载即可。
```python
import phpy
content = phpy.call('file_get_contents', 'test.txt')
o = phpy.Object('redis')
assert o.call('connect', '127.0.0.1', 6379)
rdata = phpy.call('uniqid')
assert o.call('set', 'key', rdata)
assert o.call('get', 'key') == rdata
```
实现原理
----
在进程内同时创建了 `ZendVM` 和 `CPython VM`,直接在进程堆栈空间内使用 `C` 函数互相调用,
开销只有 `zval <-> PyObject` 结构体转换,因此性能是非常高的。
在基准测试中我们创建了一个 `PyDict` ,分别读写 `PHP` 代码和 `Python` 代码执行 `1000万次`。
`phpy` 以 `PHP` 代码写入 `PyDict` 的性能比原生 `Python` 高 `14%`,读取性能高 `25%`。
> 详细细节请参考 [压力测试](docs/cn/benchmark.md)
微信交流群
----
