Asynchronous function call?
am 11.08.2007 18:47:41 von ZannaHi all
There is a way to exec a function asynchronously?
I need to exec a function that is too slow but for me can be executed in the
background... there is no way???
Thanks
Hi all
There is a way to exec a function asynchronously?
I need to exec a function that is too slow but for me can be executed in the
background... there is no way???
Thanks
On Aug 11, 12:47 pm, "Fabio"
> Hi all
>
> There is a way to exec a function asynchronously?
> I need to exec a function that is too slow but for me can be executed in the
> background... there is no way???
>
> Thanks
I think the manual on exec() makes it pretty clear:
"Note: If you start a program using this function and want to leave it
running in the background, you have to make sure that the output of
that program is redirected to a file or some other output stream or
else PHP will hang until the execution of the program ends."
On Aug 11, 11:47 am, "Fabio"
> Hi all
>
> There is a way to exec a function asynchronously?
> I need to exec a function that is too slow but for me can be executed in the
> background... there is no way???
>
> Thanks
Yes and no.
PHP, being an extreamly veristile language, can be made to do many
thing with enough imagination =). First, we really need to know what
you are doing, how, and why. By you using the term 'asynchronous', I
assume you probably a good amount about programming and threads. So,
I'm not trying to assault you're judgement, but wanting to help you in
the best way.
On way to do this is to call a/multiple CLI scripts from your base PHP
script, let them process in the background, do some more things if you
would like with your "base" script, and get back to them, reading the
results later on (if you want).
"ELINTPimp"
news:1186860780.947564.220510@k79g2000hse.googlegroups.com.. .
> PHP, being an extreamly veristile language, can be made to do many
> thing with enough imagination =). First, we really need to know what
> you are doing, how, and why. By you using the term 'asynchronous', I
> assume you probably a good amount about programming and threads. So,
> I'm not trying to assault you're judgement, but wanting to help you in
> the best way.
>
> On way to do this is to call a/multiple CLI scripts from your base PHP
> script, let them process in the background, do some more things if you
> would like with your "base" script, and get back to them, reading the
> results later on (if you want).
>
Ok, I thank you and I'll explain.
In a web site page, when someone press a submit button I should exec a
mail() function to send an email to each registered user.
The execution of mail() is a little slow, so if the user list is a little
long who pressed the button need to wait to obtain the interaction with the
site.
My idea is to exec all the calls to mail() in background, so the interation
is immediate and if the mail sending use some "hidden" minutes it's not a
problem.
I hope I was clear wth my english and newbie php language :)
Thanks again.
On Aug 11, 2:41 pm, "Fabio"
> "ELINTPimp"
>
> > PHP, being an extreamly veristile language, can be made to do many
> > thing with enough imagination =). First, we really need to know what
> > you are doing, how, and why. By you using the term 'asynchronous', I
> > assume you probably a good amount about programming and threads. So,
> > I'm not trying to assault you're judgement, but wanting to help you in
> > the best way.
>
> > On way to do this is to call a/multiple CLI scripts from your base PHP
> > script, let them process in the background, do some more things if you
> > would like with your "base" script, and get back to them, reading the
> > results later on (if you want).
>
> Ok, I thank you and I'll explain.
> In a web site page, when someone press a submit button I should exec a
> mail() function to send an email to each registered user.
> The execution of mail() is a little slow, so if the user list is a little
> long who pressed the button need to wait to obtain the interaction with the
> site.
> My idea is to exec all the calls to mail() in background, so the interation
> is immediate and if the mail sending use some "hidden" minutes it's not a
> problem.
>
> I hope I was clear wth my english and newbie php language :)
>
> Thanks again.
Yes, and your idea of splitting the process to the "background" is
good. Try calling a CLI script to do the dirty work for you.
On 11 Aug, 20:41, "Fabio"
>
> Ok, I thank you and I'll explain.
> In a web site page, when someone press a submit button I should exec a
> mail() function to send an email to each registered user.
> The execution of mail() is a little slow, so if the user list is a little
> long who pressed the button need to wait to obtain the interaction with the
> site.
> My idea is to exec all the calls to mail() in background, so the interation
> is immediate and if the mail sending use some "hidden" minutes it's not a
> problem.
>
> I hope I was clear wth my english and newbie php language :)
>
Try googling for PHP script and background execution - this gets asked
again and again. And its not nearly as simple as some people believe -
expect to do some testing/tinkering especially if you have any sort of
traffic volumnes.
C.
"ELINTPimp"
news:1186863737.691894.94050@57g2000hsv.googlegroups.com...
> Yes, and your idea of splitting the process to the "background" is
> good. Try calling a CLI script to do the dirty work for you.
I'm really new to php... what is a CLI script?
Thanks
"C."
news:1186865072.472188.92690@19g2000hsx.googlegroups.com...
>
> Try googling for PHP script and background execution - this gets asked
> again and again.
It's all the day I'm searching for in google... I didn't find nothing that I
understand :(
I also tryed looking in the phpBB code... don't let me tell about it...
> And its not nearly as simple as some people believe -
> expect to do some testing/tinkering especially if you have any sort of
> traffic volumnes.
Yes, it's my fear for the future... :(
On Aug 11, 3:54 pm, "Fabio"
> "ELINTPimp"
>
> > Yes, and your idea of splitting the process to the "background" is
> > good. Try calling a CLI script to do the dirty work for you.
>
> I'm really new to php... what is a CLI script?
>
> Thanks
Sorry about my short message yesterday, I was running out the door.
This article seems pretty straight forward:
http://www.webforumz.com/php-forum/12595-multithreaded-php.h tm
> And its not nearly as simple as some people believe -
> expect to do some testing/tinkering especially if you have any sort of
> traffic volumnes.
I've done it, the PHP side isn't too hard. Unit testing should be done
on a lot of the methods we write, and shouldn't be limited things like
this. Yes, test. Test everything.
"ELINTPimp"
news:1186923077.968478.215140@d55g2000hsg.googlegroups.com.. .
> Sorry about my short message yesterday, I was running out the door.
No problems, it happens ;)
>
> This article seems pretty straight forward:
>
> http://www.webforumz.com/php-forum/12595-multithreaded-php.h tm
>
I found something similar that uses open_proc() instead of popen() (from php
manual I think there is no big differences)
http://www.alternateinterior.com/2007/05/benchmarking-php-th reads.html
but there are some questions in all the situation:
- when I launch a process I need always to close it with
pclose()/close_proc()?
And how can I know when I must close it if I lounch the process in
background???
The process don't close itself when it finishes?
- I can pass the parameter to the process in something as
$process = popen("/usr/local/bin/php ". $php_script_file . " " .
$parameter, "r");
Ok, but for a mail the parameter can be large!
email address, subject, body... can I pass it in this way???
Or there is a smarter way?
- In the example there is the path of the php ("/usr/local/bin/php").
But php may stay in another directory... Can I know where it is?
I thank you again!
The system could also run your command for you when you use the at
command.
For example, this page launch.php calls the at command, which loads
the web page batch.php in the background:
// File: launch.php
function launch($url)
{
$cmd = 'echo lynx -source ' . escapeshellarg($url) .
' | at now 2>&1';
exec($cmd, $output, $exitCode);
if ($exitCode != 0) {
$msg = "Command \"$cmd\" failed with exit code $exitCode: ";
$msg .= join("\n", $output);
trigger_error($msg, E_USER_ERROR);
return false;
}
return true;
}
$done = false;
if (isset($_GET['btn'])) {
$url = 'http://my.tld/batch.php';
$done = launch($url);
}
?>
Command launched in background.
On 11.08.2007 21:41 Fabio wrote:
> "ELINTPimp"
> news:1186860780.947564.220510@k79g2000hse.googlegroups.com.. .
>
>> PHP, being an extreamly veristile language, can be made to do many
>> thing with enough imagination =). First, we really need to know what
>> you are doing, how, and why. By you using the term 'asynchronous', I
>> assume you probably a good amount about programming and threads. So,
>> I'm not trying to assault you're judgement, but wanting to help you in
>> the best way.
>>
>> On way to do this is to call a/multiple CLI scripts from your base PHP
>> script, let them process in the background, do some more things if you
>> would like with your "base" script, and get back to them, reading the
>> results later on (if you want).
>>
>
> Ok, I thank you and I'll explain.
> In a web site page, when someone press a submit button I should exec a
> mail() function to send an email to each registered user.
> The execution of mail() is a little slow, so if the user list is a little
> long who pressed the button need to wait to obtain the interaction with the
> site.
> My idea is to exec all the calls to mail() in background, so the interation
> is immediate and if the mail sending use some "hidden" minutes it's not a
> problem.
>
> I hope I was clear wth my english and newbie php language :)
>
> Thanks again.
>
>
I think you'd be better off having a mail sender cronjob running
independently from your web application. The application only creates a
working set for the cronjob (e.g. populates a database table or similar).
--
gosha bine
makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
On Aug 13, 3:51 am, gosha bine
> On 11.08.2007 21:41 Fabio wrote:
>
>
>
> > "ELINTPimp"
> >news:1186860780.947564.220510@k79g2000hse.googlegroups.com. ..
>
> >> PHP, being an extreamly veristile language, can be made to do many
> >> thing with enough imagination =). First, we really need to know what
> >> you are doing, how, and why. By you using the term 'asynchronous', I
> >> assume you probably a good amount about programming and threads. So,
> >> I'm not trying to assault you're judgement, but wanting to help you in
> >> the best way.
>
> >> On way to do this is to call a/multiple CLI scripts from your base PHP
> >> script, let them process in the background, do some more things if you
> >> would like with your "base" script, and get back to them, reading the
> >> results later on (if you want).
>
> > Ok, I thank you and I'll explain.
> > In a web site page, when someone press a submit button I should exec a
> > mail() function to send an email to each registered user.
> > The execution of mail() is a little slow, so if the user list is a little
> > long who pressed the button need to wait to obtain the interaction with the
> > site.
> > My idea is to exec all the calls to mail() in background, so the interation
> > is immediate and if the mail sending use some "hidden" minutes it's not a
> > problem.
>
> > I hope I was clear wth my english and newbie php language :)
>
> > Thanks again.
>
> I think you'd be better off having a mail sender cronjob running
> independently from your web application. The application only creates a
> working set for the cronjob (e.g. populates a database table or similar).
>
> --
> gosha bine
>
> makrell ~http://www.tagarga.com/blok/makrell
> php done right ;)http://code.google.com/p/pihipi
cronjob's a good idea, especially if you don't need the mailing
executed dynamically from a web page. But it seems like he wants to
send a notification email, or something of the sort, to all the
registered users when a ticket is submitted (something like a helpdesk
system, or something of the sort). If the speed of the submission of
a email message is "time-critical" (we all know we really shouldn't
DEPEND on email on the Internet, but often times it's an important
component), perhaps a cronjob isn't really the solution. You could
set your cron to check every 5min or so, but then your delayed 5min
and there is overhead in calling the DB at least once (maybe twice to
check if there are messages pending and once to query users).
as always, it comes down to what you need for your particular
situation.
gosha bine wrote:
> I think you'd be better off having a mail sender cronjob running
> independently from your web application. The application only creates a
> working set for the cronjob (e.g. populates a database table or similar).
Yep, but this is re-inventing the wheel.
Just need to run an SMTP daemon on localhost:25. If the PHP mail()
function is only sending to localhost for further relaying, it will
return a result very quickly. Then let the mailer daemon worry
about how long it takes to actually send the message.
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 54 days, 20:31.]
Fake Steve is Dead; Long Live Fake Bob!
http://tobyinkster.co.uk/blog/2007/08/13/fake-bob/