我想在 php 中定义一个具有超全局作用域的变量
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将变量写入特定的内存地址,并使用计数等信息读写该地址可能会起作用。
关于 IPC(进程间通信)的(旧)文章可能会帮助您
http://zez.org/article /articleview/46/
出于兴趣,为什么您不想使用文件或数据库?
另一种解决方案(尽管技术上很可能使用文件或数据库)是强制脚本使用特定的会话 ID。
请注意,在上面的示例中,对 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.
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
没有办法将其保存在其他地方,供没有文件或数据库的每个人使用
there's no way to save this somewhere else for everyone without file nor DB
你的前提条件相当有限。内存不是(或不是通常)在脚本之间共享,我们不能使用文件或数据库:这实际上没有留下可用于共享信息的公共空间,所以你每次都必须计算信息。您最好的选择可能是检查正在运行的进程,确定哪些进程属于您的脚本并对它们进行计数。你提到的 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.
据我所知,如果没有数据库或文件,这是不可能的。相反,如果您有权访问服务器(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.