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
-
are you triggering the code as a web page, or as a cron job? – BugFinder Jul 04 '11 at 16:50
-
1PHP isn't intended for real-time processing. Unless you're on a cooperatively multitasked system (e.g. Win 3.x), at some point the script's timeslice will be exhausted and control taken away from it. – Marc B Jul 04 '11 at 16:54
-
Answering BugFinder question: I am starting the code from a web page button. – gerardg Jul 04 '11 at 17:30
-
Answering Mark B: Thank you for your perspective. Cyclone below has offered set_time_limit($time) which may extend my time. But extended time is different than a continuous run. Jahufar below has come to your same conclusion. I am currently developing on my ISP's machine. I am waiting for my own server to be able to test and schedule myself back. Thank you. – gerardg Jul 04 '11 at 22:25
4 Answers
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

- 31
- 1
-
usleep doesn't active like doevents since it only delays execution rather than yielding control back to other processes. I open a page tab to test a php which output js codes to alter contents of document title with a counter value and use usleep to delay per 0.01s but I can't click other page tabs while this test program is still updating the document title. – Scott Chu Mar 28 '12 at 22:47
-
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.

- 6,271
- 3
- 43
- 62
-
Answering Jahufar: Thank you. You understand my question. Both you and Marc B are saying PHP doesn't have that functionality. It looks like I might be able to reschedule my self with the process control functions you sent. I am going to test Cyclone's recommendation for set_time_limit($time), other functions, and your control functions when I get on my own server. (I'm developing on my ISP's machine and can't control the system as I would like.) I will return back to here with an update after my server arrives and I can experiment. Thank you. – gerardg Jul 05 '11 at 00:13
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.

- 17,939
- 45
- 124
- 193
-
Answering Cyclone: Thank you. You have certainly researched this. I am still in development on my ISP's server and can't control everything until my server arrives. I think with your recommendations I can cut down on the number of times I will need to re-schedule my process. I will practice with the functions you recommend and implement them later on my own server in hopes of staying in memory and not rescheduling myself. I think with your recommendations and Jahufar's list of process control functions (if I do have to reschedule) I can achieve the DoEvents functionality. Thank you. – gerardg Jul 05 '11 at 00:32
-
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.

- 2,298
- 1
- 27
- 40