定制 swoole/phpy 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

swoole/phpy

Composer 安装命令:

composer require swoole/phpy

包简介

Connecting the Python and PHP ecosystems together

README 文档

README

简体中文

phpy

PHP Tests Python Tests Codecov

A library for inter-calling Python and PHP. You can use Python functions and libraries in PHP, or use PHP packages in Python.

phpy ecosystems

  • See documents: docs/en/README.md
  • Supports Linux/Windows/macOS
  • Not support Python threading or async-io features
  • Require PHP 8.1 or later version

py2php

py2php is online utility that will auto-translate python code into PHP code.

Calling Python from PHP

Compile and install phpy.so as an extension, and append extension=phpy.so to php.ini.

PHP Example:

$os = PyCore::import("os");
echo $os->uname();

Transformers

$transformers = PyCore::import('transformers');
$AutoTokenizer = $transformers->AutoTokenizer;
$AutoModelForSequenceClassification = $transformers->AutoModelForSequenceClassification;

$os = PyCore::import('os');
$os->environ['https_proxy'] = getenv('https_proxy');

$tokenizer = $AutoTokenizer->from_pretrained("lxyuan/distilbert-base-multilingual-cased-sentiments-student");
$model = $AutoModelForSequenceClassification->from_pretrained("lxyuan/distilbert-base-multilingual-cased-sentiments-student");

Calling PHP from Python

Simply import it as a C++ Mudule.

Python Example:

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

Implementation

It creates ZendVM and CPython VM in the process at the same time, and directly uses C functions to call each other in the process stack space.

The overhead is only the conversion of zval <-> PyObject structures, so the performance is very high.

In the benchmark test, we created a PyDict and executed PHP code and Python code to read and write 10 million times respectively.

The performance of phpy writing PyDict with PHP code is 14% higher than the native Python, and the read performance is 25% higher.

More details: docs/en/benchmark.md

swoole/phpy 适用场景与选型建议

swoole/phpy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.06k 次下载、GitHub Stars 达 649, 最近一次更新时间为 2023 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「python」 「ai」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 swoole/phpy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 swoole/phpy 我们能提供哪些服务?
定制开发 / 二次开发

基于 swoole/phpy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 650
  • 点击次数: 18
  • 依赖项目数: 1
  • 推荐数: 1

GitHub 信息

  • Stars: 649
  • Watchers: 16
  • Forks: 53
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2023-12-11