简体   繁体   English

在 python 和 php 之间共享变量

[英]Share variables between python & php

I have a script in python receiving continuously data from sensors.我在python 中有一个脚本,不断从传感器接收数据。

I need to publish the last data at request on a webpage using php .我需要使用php在网页上根据请求发布最后一个数据。

Apache, php and python are all on the same linux install (actually on an raspberry). Apache、php 和 python 都在同一个 linux 安装上(实际上是在树莓上)。

I'm not interested in storing previous data and I'm a bit concerned about data corruption on writing on SD.我对存储以前的数据不感兴趣,而且我有点担心在 SD 上写入时数据损坏。 I would prefer to reduce complexity and increase speed refresh (avoid sql).我更愿意降低复杂性并提高刷新速度(避免使用 sql)。

Could a text file written in ramfs / tmpfs solve the problem?ramfs / tmpfs编写的文本文件可以解决问题吗? or there is a method to share memory like memcache to share also global variables?或者有一种方法可以像memcache这样共享内存来共享全局变量?

在此处输入图片说明

Any practical example or howto will be really well-accepted.任何实际的例子或方法都会被真正接受。

You can use any interoperable format like json or msgpack.您可以使用任何可互操作的格式,例如 json 或 msgpack。 Whenever you generate data in python, add it to a caching layer like memcache/redis using json format ( and preferably a gzip compressed version), then your PHP script can unserialize the json data and display it in the app.每当您在 python 中生成数据时,使用 json 格式(最好是 gzip 压缩版本)将其添加到 memcache/redis 等缓存层,然后您的 PHP 脚本可以反序列化 json 数据并将其显示在应用程序中。

Clearly memcache as a means to share data different application will work.显然,memcache 作为一种共享数据的方式可以在不同的应用程序中工作。 You will not have any corrupted data for sure as all the memcache operation are atomic.您肯定不会有任何损坏的数据,因为所有内存缓存操作都是原子的。 memcache atomic discussion could be useful. memcache 原子讨论可能很有用。

On memcached's FAQ:关于 memcached 的常见问题:

Is memcached atomic? memcached 是原子的吗? Aside from any bugs you may come across, yes all commands are internally atomic.除了您可能会遇到的任何错误之外,所有命令都是内部原子的。 Issuing multiple sets at the same time has no ill effect, aside from the last one in being the one that sticks.同时发行多套没有任何不良影响,除了最后一套是坚持的那一套。

Note: Running memcache service might consume considerable amount of memory.注意:运行内存缓存服务可能会消耗大量内存。

Hope it helps!希望有帮助!

I think you may try System V shared memory.我想你可以试试 System V 共享内存。

As example:例如:

In Python side: python -m pip install sysv_ipc在 Python 端: python -m pip install sysv_ipc

then somewhere in the python script:然后在python脚本中的某处:

import sysv_ipc
...
KEY=20171220
sysv_memory=sysv_ipc.SharedMemory(KEY, sysv_ipc.IPC_CREAT, 0666, 256)
...
sysv_memory.write('1234'+'\0')

Then in the PHP side:然后在 PHP 端:

$SHARED_MEMORY_KEY = 20171220;
...
$shmId = shmop_open($SHARED_MEMORY_KEY, 'a', 0666, 256);
...
$test_string = shmop_read($shmId, 0, 0);

I can get the $test_string as '1234' successfully in PHP.我可以在 PHP 中成功获取 $test_string 为“1234”。

Here is a solution using memcached that works on Raspbian 10 (buster), PHP 7.3.19, and Python 3.7.3:这是一个使用 memcached 的解决方案,适用于 Raspbian 10 (buster)、PHP 7.3.19 和 Python 3.7.3:

1. Install memcached 1.安装memcached

apt-get install memcached php-memcached
pip install pymemcache

These commands install memchached server and client modules for PHP and Python.这些命令为 PHP 和 Python 安装 memchached 服务器和客户端模块。

2. PHP code 2.PHP代码

$m = new Memcached();

// connect
$m->addServer('127.0.0.1', 11211);

// get a value
$value = $m->get('key');

// set a value
$m->set('key', 'value');

// clean up
$m->quit();

3. Python code 3.Python代码

from pymemcache.client import base

# connect
m = base.Client(('127.0.0.1', 11211))

# get a value
value = m.get('key')

# set a value
m.set('key', 'value')

# clean up
m.close()

Note:注意:

I used default memcached settings here.我在这里使用了默认的 memcached 设置。 If you need to change them edit sudo nano /etc/memcached.conf and restart the daemon: sudo /etc/init.d/memcached restart .如果您需要更改它们,请编辑sudo nano /etc/memcached.conf并重新启动守护进程: sudo /etc/init.d/memcached restart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM