windows-10 - “shmop_open():无法附加或创建共享内存段'无错误':”?
问题描述
每次我尝试创建一个帐户以在 Stack Overflow 上询问这个问题时,我都会得到这个:
哎呀!坏事发生了!
对于给您带来的不便,我们深表歉意,但在您浏览我们的网站时发生了意外错误。
不是你,是我们。这是我们的错。
这就是我在这里发布它的原因。我真的无法在 Overflow 上问它,即使在我一天中(断断续续)花费数小时重复我的尝试并解决了一百万个 reCAPTCHA 难题之后。你能尽快修复这个错误吗?
由于没有有意义/完整的示例,并且基本上没有任何文档,我多年来一直在尝试使用 PHP 的“shmop”部分。现在我必须找到一种方法来在同一台机器上运行的两个不同的 CLI PHP 脚本之间发送数据,而不会为此滥用数据库。它必须在没有数据库支持的情况下工作,这意味着我正在尝试使用 shmop,但它根本不起作用:
$shmopid = shmop_open(1, 'w', 0644, 99999); // I have no idea what the "key" is supposed to be. It says: "System's id for the shared memory block. Can be passed as a decimal or hex.", so I've given it a 1 and also tried with 123. It gave an error when I set the size to 64, so I increased it to 99999. That's when the error changed to the one I now face above.
shmop_write($shmopid, 'meow 123', 0); // Write "meow 123" to the shared variable.
while (1)
{
$shared_string = shmop_read($shmopid, 0, 8); // Read the "meow 123", even though it's the same script right now (since this is an example and minimal test).
var_dump($shared_string);
sleep(1);
}
我收到第一行的错误:
shmop_open(): unable to attach or create shared memory segment 'No error':
这意味着什么?我究竟做错了什么?为什么手册对此如此神秘?为什么这不仅仅是一个可以跨脚本访问的内置“超级数组”?
解决方案
About CLI:
It cannot work in standalone CLI processes, as an answer here says: https://stackoverflow.com/a/34533749
The master process is the one to hold the shared memory block, so you will have to use php-fpm
or mod_php
or some other web/service-running version, and maybe even start/request/stop it all from a CLI php script.
About shmop usage itself:
Use "c" mode in shmop_open()
for creating the segment before it can be used with "a" or "w".
I stumbled on this error in a different scenario where shared memory is completely optional to speed up some repeated operations. So I wanted to try reading first without knowing memory size and then allocate from actual data when needed. In my case I had to call it as @shmop_open()
to hide this error output.
About shmop on Windows:
PHP 7 crashed Apache worker process causing its restart with status 3221225477 when trying to reallocate a segment with the same predefined (arbitrary number) key but different size, even after shmop_delete()
. As a workaround for this, I took filemtime()
of the source file which contains data to be stored in memory, and used that timestamp as the key in shmop_open()
. It still was not flawless IIRC, and I don't know if it would cause memory leaks, but it was enough to test my code which would mainly run on Linux anyway.
Finally, as of PHP version 8.0.7, shmop seems to work fine with Apache 2.4.46 and mod_php
in Windows 10.
推荐阅读
- python - 并行调用函数的并行化替代方案?
- python - 如何以安全的方式从外部访问 PostgreSQL 数据库
- r - r: 使用 purrr::safely 处理网页抓取失败的 url
- c# - 我的客户端程序无法连接到我的服务器程序
- c# - 使用角色控制器使物体在靠近边缘时下落
- amazon-web-services - 为什么 AWS ECS 有 10240 个 CPU 单元的限制?
- amazon-s3 - Lambda@Edge Cache-Control 标头不存在 Cloudfront
- r - R, sapply 似乎没有应用提供的功能
- typescript - 静态类的 TypeScript 索引器
- webpack-dev-server - 是否有在本地使用视频(.mov、.mp4)和带有“文件加载器”的 Webpack 的特殊配置?