我正在寻找 VB doevents 的 PHP 等效项

发布于 2024-11-18 10:26:05 字数 211 浏览 3 评论 0 原文

我正在寻找 VB doevents 的 PHP 等效项。 我用VB编写了一个实时分析包,并使用doevents发布到操作系统。 Doevents 允许我保留在内存中并连续运行而不会填满内存,并允许我响应用户输入。 我已经用 PHP 重写了该包,并且正在寻找相同的 doevents 功能。 如果它不存在,我可以重新安排自己并退出。 但我目前不知道如何做到这一点,并且我认为这会增加更多的开销。 谢谢你,杰拉德

I am looking for the PHP equivalent for VB doevents.
I have written a realtime analysis package in VB and used doevents to release to the operating system.
Doevents allows me to stay in memory and run continuously without filling up memory and allows me to respond to user input.
I have rewritten the package in PHP and I am looking for that same doevents feature.
If it doesn't exist I could reschedule myself and exit.
But I currently don't know how to do that and I think that would add a lot more overhead.
Thank you, gerardg

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

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

发布评论

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

评论(4

信仰 2024-11-25 10:26:05

usleep 是您正在寻找的..将程序执行延迟给定的微秒数
http://php.net/manual/en/function.usleep.php

usleep is what you are looking for.. Delays program execution for the given number of micro seconds
http://php.net/manual/en/function.usleep.php

挽心 2024-11-25 10:26:05

距离我上次在 VB 中编写任何内容已经过去了近 10 年,我记得 doevents() 函数允许应用程序在密集处理期间屈服于处理器(通常允许其他系统事件触发 - 最常见的是 WM_PAINT,以便您的UI 不会显示为挂起)。

我不认为 PHP 具有这样的功能 - 您的脚本将作为单个进程运行并结束(无论是在完成时还是在达到默认的 30 秒超时时)。

如果您正在考虑线程(正如大多数 Windows 程序员倾向于做的那样)并且需要生成 1 个以上的脚本实例,也许您应该看看 PHP 的进程控制函数作为开始。

It's been almost 10 years since I last wrote anything in VB and as I recall, doevents() function allowed the application to yield to the processor during intensive processing (usually to allow other system events to fire - the most common being WM_PAINT so that your UI won't appear hung).

I don't think PHP has such functionality - your script will run as a single process and end (either when it's done or when it hits the default 30 second timeout).

If you are thinking in terms of threads (as most Windows programmers tend to do) and needing to spawn more than 1 instance of your script, perhaps you should take look at PHP's Process Control functions as a start.

小…红帽 2024-11-25 10:26:05

我不完全确定您想要模拟 doevents 的哪些方面,因此这里几乎包含了对您有用的所有内容。

您可以使用 ob_implicit_flush(true) 在脚本顶部启用隐式输出缓冲区刷新。这意味着每当您的脚本调用 echoprint 或任何您用来显示内容的内容时,PHP 都会自动将其全部发送到用户的浏览器。您也可以只使用 ob_flush() 每次调用显示内容后,其作用更像 VB 中的 Application.DoEvents() 来保持 UI 活动,但每次输出内容时都必须调用。

当然,如果您的脚本已经使用输出缓冲区,您可以在刷新之前构建缓冲区的副本,使用 ob_get_contents()

如果您需要允许脚本运行比平时更长的时间,您可以使用 set_time_limit($time)。如果您需要更多内存,并且可以编辑 .htaccess 文件,请放置以下代码并编辑值:

php_value memory_limit 64M

这会将内存限制设置为 64 MB。

要一次运行多个脚本,您可以使用 pcntl_exec 启动另一个运行。

如果我遗漏了有关 DoEvents() 的重要信息,请告诉我,我将尽力帮助您使其正常工作。

I'm not entirely sure which aspects of doevents you're looking to emulate, so here's pretty much everything that could be useful for you.

You can use ob_implicit_flush(true) at the top of your script to enable implicit output buffer flushing. That means that whenever your script calls echo or print or whatever you use to display stuff, PHP will automatically send it all to the user's browser. You could also just use ob_flush() after each call to display something, which acts more like Application.DoEvents() in VB with regards to keeping your UI active, but must be called each time something is output.

Naturally if your script uses the output buffer already, you could build a copy of the buffer before flushing, with ob_get_contents().

If you need to allow the script to run for more time than usual, you can set a longer tiemout with set_time_limit($time). If you need more memory, and you have access to edit your .htaccess file, place the following code and edit the value:

php_value memory_limit 64M

That sets the memory limit to 64 megabytes.

For running multiple scripts at once, you can use pcntl_exec to start another one running.

If I am missing something important about DoEvents(), let me know and I will try to help you make it work.

枯寂 2024-11-25 10:26:05

PHP 专为异步按需处理而设计。然而,只需一点技巧,它就可以被迫成为后台任务。

由于 PHP 作为单线程运行,因此您不必担心让 CPU 执行其他操作,因为这已经得到处理。如果不是这种情况,那么 Web 服务器一次只能提供一页,所有其他请求都必须排队。您将需要编写某种在发生某些可检测条件之前永远不会过期的外观(例如您在数据库中设置的“现在请退出”消息或其他消息)。

正如其他人所指出的,如果每个循环占用大量 CPU,您将需要 set_time_limit($something); 以及 usleep 来阻止代码运行“太快”。但是,如果您还使用数据库连接,则大部分脚本时间实际上是等待数据库的脚本(迄今为止脚本的最大开销)。

我见过通过使用 screen 创建 PHP 工作线程并将其分离到后台任务。只要您没有超时或退出的会话(例如,当 Web 浏览器关闭时),其他方法也可以使用。启动脚本来检查脚本是否每 x 分钟或小时运行一次的 cron 可以让您从强制存在和/或系统重新启动中自动恢复。

TL;DR:doevents 已“内置”到 PHP 中,您不必担心它。

PHP is designed for asynchronous on demand processing. However it can be forced to become a background task with a little hackery.

As PHP is running as a single thread you do not have to worry about letting the CPU do other things as that is already taken care of. If this was not the case then a web server would only be able to serve up one page at a time and all other requests would have to sit in a queue. You will need to write some sort of look that never expires until some detectable condition happens (like the "now please exit" message you set in the DB or something).

As pointed out by others you will need to set_time_limit($something); with perhaps usleep stopping the code from running "too fast" if it eats very much CPU each loop. However if you are also using a Database connection most of your script time is actually the script waiting for the Database (by far the biggest overhead for a script).

I have seen PHP worker threads created by using screen and detatching it to a background task. Other approaches also work so long as you do not have a session that will time out or exit (say when the web browser is closed). A cron that starts a script to check if the script is running every x mins or hours gives you automatic recovery from forced exists and/or system restarts.

TL;DR: doevents is "baked in" to PHP and you don't have to worry about it.

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