You are here: Re: Launch a background task from a PHP script? « PHP Programming Language « IT news, forums, messages
Re: Launch a background task from a PHP script?

Posted by Michael Austin on 10/20/53 11:40

Francois Bonzon wrote:

> Any idea how I can launch a background task from a PHP script?
>
> For example, when a user posts on my message board, it may fire many
> e-mail notifications to other users, and other tasks. I want the posting
> confirmation page to end displaying quickly, without waiting for those
> notifications or other tasks to complete.
>
> So basically what I want is to launch 'background.php' from 'main.php',
> with the user seeing only the output of - and waiting only for the end
> of - 'main.php'. Without the need to wait for the end of the possibly
> long 'background.php', like if I had simply included it in 'main.php'.
>
> According to the PHP Manual, the function register_shutdown_function()
> was the solution "in PHP 4.0.6 and earlier under Apache", but "since PHP
> 4.1 the shutdown functions are called as the part of the request" so
> that PHP waits until the shutdown functions are completed before closing
> the connection to the user.
>
> E.g. in PHP 4.1+ this
>
> <?php
> function background () {
> echo 'Starting background task... ';
> // Do something long that I don't want the user to see or wait for
> sleep(5);
> echo 'End of background task. ';
> }
>
> echo 'Starting main script... ';
> register_shutdown_function('background');
> echo 'End of main script. ';
> ?>
>
> will output:
>
> Starting main script... End of main script. Starting background task...
> End of background task.
>
> I want a way to output only:
>
> Starting main script... End of main script.
>
> and do the remaining in background.
>


What platform are you on?

if this is UNIX then you can do an exec() or system() call or something like
`./executethisfile > out.log &`
note these are back-ticks (key on the top row far left ~` )

the "&" tells the shell script to execute in the background. Not sere how you
would accomplish this on the Windows platform, but then again if you are doing
anything serious, Windows is not your server platform.

--
Michael Austin.
DBA Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация