我想在 php 中定义一个具有超全局作用域的变量

发布于 2024-11-30 08:23:00 字数 273 浏览 1 评论 0原文

我想在 php 中定义一个全局范围的变量。该变量应该可供每个用户访问,因此它不应该依赖于会话。

我想做如下的事情:我有一个 php 脚本,该脚本通常需要大约 5 到 15 分钟来执行,但在某些特殊情况下可能需要一个多小时。该脚本正在使用 cron 作业以 15 分钟的间隔运行。该脚本也可以由任何用户手动运行。

现在可以同时运行任意数量的脚本。我想限制这个脚本数量。

所以,我想要一个变量来存储当前正在执行的脚本的数量。我不想使用数据库或文件来存储单个值。

提前致谢。

I want to define a variable in php with in a global scope. That variable should be accessible for each user, so it should not depend on the session.

I want to do something like the following: I have a php script, and that script usually takes about 5 to 15 minutes to execute, but it can take more than one hour in some special cases. This script is being run using a cron job with a 15 minutes interval. This script can also be run manually by any user.

Now it's possible to run any number of scripts at the same time. I want to limit this number of scripts.

So, I want a variable to store the number of currently executing scripts. I don't want to use a DB or files to store a single value.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦晓ヶ微光ヅ倾城 2024-12-07 08:23:00

将变量写入特定的内存地址,并使用计数等信息读写该地址可能会起作用。

关于 IPC(进程间通信)的(旧)文章可能会帮助您

http://zez.org/article /articleview/46/

出于兴趣,为什么您不想使用文件或数据库?

另一种解决方案(尽管技术上很可能使用文件或数据库)是强制脚本使用特定的会话 ID。

<?php

$maximum = 1;

session_id(md5('myscript'));
session_start();

if( !isset( $_SESSION['count'] ) ) {
    $_SESSION['count'] = 1;
} else {
    if( $_SESSION['count'] >= $maximum ) {
        die( "too many processes running" );
    } else {
        $_SESSION['count']++;
    }
}

session_write_close();

// simulate running something
sleep(10);

session_id(md5('myscript'));
session_start();

$_SESSION['count']--;

session_write_close();

echo "<br />executed";

?>

请注意,在上面的示例中,对 session_write_close() 的调用很重要,因为脚本的执行将阻止计数器递增,直到它完成为止。然后当它关闭它时,它需要重新打开以减少计数器。

显然,由于会话重新打开,脚本的任何输出都需要缓冲(请参阅 http://www.php. net/ob_start)以防止标头已发送错误

Writing the variables to a specific memory address and reading and writing to/from that address with information such as a count might work.

An (old) article on IPC (inter process communication) might help you

http://zez.org/article/articleview/46/

Out of interest why is it you don't want to use files or a database?

Another solution (despite more than likely technically using files or a database) would be to force the script to use a specific session ID.

<?php

$maximum = 1;

session_id(md5('myscript'));
session_start();

if( !isset( $_SESSION['count'] ) ) {
    $_SESSION['count'] = 1;
} else {
    if( $_SESSION['count'] >= $maximum ) {
        die( "too many processes running" );
    } else {
        $_SESSION['count']++;
    }
}

session_write_close();

// simulate running something
sleep(10);

session_id(md5('myscript'));
session_start();

$_SESSION['count']--;

session_write_close();

echo "<br />executed";

?>

Note in the example above, the call to session_write_close() is important because the execution of the script will prevent the counter being incremented until it finishes otherwise. And then as it closes it, it needs reopening to decrement the counter.

Obviously because of the session reopening, any output from the script would need to be buffered (see http://www.php.net/ob_start) to prevent headers already sent errors

怪我鬧 2024-12-07 08:23:00
  1. 是否达到限制
  2. 检查用户访问脚本时
  3. ,当用户存在(或脚本结束)时将其添加到数据库,将其从数据库中删除

没有办法将其保存在其他地方,供没有文件或数据库的每个人使用

  1. check if limit is reached
  2. when user access script, add him to DB
  3. when user exists (or script ends), remove him from DB

there's no way to save this somewhere else for everyone without file nor DB

万劫不复 2024-12-07 08:23:00

你的前提条件相当有限。内存不是(或不是通常)在脚本之间共享,我们不能使用文件或数据库:这实际上没有留下可用于共享信息的公共空间,所以你每次都必须计算信息。您最好的选择可能是检查正在运行的进程,确定哪些进程属于您的脚本并对它们进行计数。你提到的 cron 暗示了 Unix,这让事情变得更容易。 POSIX 扩展可能允许在这种程度上控制流程执行,并且,最后一个资源,您始终可以从 PHP 运行 ps 命令。

Your preconditions are pretty restrictive. Memory is not (or not normally) shared between scripts and we cannot use files or databases: that virtually leaves no common space available to share information, so you have to calculate the information every time. Your best option is probably to inspect the running processes, identify which ones belong to your script and count them. Your mention to cron suggests Unix, which makes things easier. The POSIX extension probably allows to control process execution to this degree and, as last resource, you can always run the ps command from PHP.

行至春深 2024-12-07 08:23:00

据我所知,如果没有数据库或文件,这是不可能的。相反,如果您有权访问服务器(root 访问权限),则可以使用 ps 命令(如果运行 Linux 操作系统)检查脚本是否已在运行,如果是,您可以查看有多少实例它的。

Without DB or files this isn't possible as far as I am aware of. Instead, if you have access to the server (root access) you could check with the ps command (if running a linux OS) if the script is already running and if so, you could see how much instances of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文