phpy Python and PHP inter-call library

phpy Python and PHP inter-call library

Software Description

phpy is a library for interoperability between PythonPHP and Python . You can use the functions and class libraries of the Python language in Python, or use the packages of Python in Python. However, it is not a language embedder. The encoding still uses the native syntax of each language.PHPPHPPythonPythonPHP

phpyIt allows PHPto call all Python‘s packages, including the currently very popular PyTorchtransformersTensorFlowand other ‘ AIlibraries, as well as scientific computing libraries such as NumpyPandas, and can also use graphical interface libraries such as and .ScikitPyQtwxPython

  • Currently only supports Linux platform (theoretically can support all operating systems, to be implemented)
  • Does not support Python multithreading, async-iofeatures

PHP calls Python

Compile and install, phpy.soload as an extension, and modify php.iniand append extension=phpy.so.

example:

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

Calling PHP from Python

Just C++ Muduleimport and load it.

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 principle

ZendVMand are created at the same time in the process CPython VM, and the functions are called each other directly in the process stack space C. The only overhead is zval <-> PyObjectthe conversion of the structure, so the performance is very high.

actual case

Implementation -based example tkinter  GUI 

<?php
$tkinter = PyCore::import('tkinter');
$root = $tkinter->Tk();
$root->title('我的窗口');
$root->geometry("500x500");
$root->resizable(False, False);

$button = $tkinter->Button($root, text: "Click Me!!", command: PyCore::fn(function () {
    var_dump(func_get_args());
    echo 'click me!!' . PHP_EOL;
}));
$button->pack();

$tkinter->mainloop();

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply