SearchSearch   

Asynchronous function call?

 
   Webmaster Forums (Home) -> PHP RSS
Next:  Subject: php mail function - use different subjec..  
Author Message
Fabio

External


Since: Aug 11, 2007
Posts: 8



(Msg. 1) Posted: Sat Aug 11, 2007 6:47 pm
Post subject: Asynchronous function call?
Archived from groups: comp>lang>php (more info?)

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
Back to top
ZeldorBlat

External


Since: Jun 20, 2007
Posts: 141



(Msg. 2) Posted: Sat Aug 11, 2007 7:14 pm
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

On Aug 11, 12:47 pm, "Fabio" <znt.fa....RemoveThis@virgilio.it> wrote:
> 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:

<http://www.php.net/exec>

"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."
Back to top
ELINTPimp

External


Since: Jun 30, 2007
Posts: 42



(Msg. 3) Posted: Sat Aug 11, 2007 7:33 pm
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

On Aug 11, 11:47 am, "Fabio" <znt.fa... DeleteThis @virgilio.it> wrote:
> 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).
Back to top
ELINTPimp

External


Since: Jun 30, 2007
Posts: 42



(Msg. 4) Posted: Sat Aug 11, 2007 8:22 pm
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

On Aug 11, 2:41 pm, "Fabio" <znt.fa... DeleteThis @virgilio.it> wrote:
> "ELINTPimp" <smsi... DeleteThis @gmail.com> ha scritto nel messaggionews: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 Smile
>
> 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.
Back to top
C.

External


Since: Jul 26, 2007
Posts: 52



(Msg. 5) Posted: Sat Aug 11, 2007 8:44 pm
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

On 11 Aug, 20:41, "Fabio" <znt.fa... DeleteThis @virgilio.it> wrote:
>
> 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 Smile
>

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.
Back to top
Fabio

External


Since: Aug 11, 2007
Posts: 8



(Msg. 6) Posted: Sat Aug 11, 2007 9:41 pm
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

"ELINTPimp" <smsiebe.RemoveThis@gmail.com> ha scritto nel messaggio
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 Smile

Thanks again.
Back to top
Fabio

External


Since: Aug 11, 2007
Posts: 8



(Msg. 7) Posted: Sat Aug 11, 2007 10:54 pm
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

"ELINTPimp" <smsiebe RemoveThis @gmail.com> ha scritto nel messaggio
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
Back to top
Fabio

External


Since: Aug 11, 2007
Posts: 8



(Msg. 8) Posted: Sat Aug 11, 2007 10:58 pm
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

"C." <colin.mckinnon.RemoveThis@gmail.com> ha scritto nel messaggio
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 Sad
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... Sad
Back to top
ELINTPimp

External


Since: Jun 30, 2007
Posts: 42



(Msg. 9) Posted: Sun Aug 12, 2007 11:44 am
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

On Aug 11, 3:54 pm, "Fabio" <znt.fa... RemoveThis @virgilio.it> wrote:
> "ELINTPimp" <smsi... RemoveThis @gmail.com> ha scritto nel messaggionews: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

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.htm

> 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.
Back to top
Fabio

External


Since: Aug 11, 2007
Posts: 8



(Msg. 10) Posted: Sun Aug 12, 2007 6:55 pm
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

"ELINTPimp" <smsiebe.RemoveThis@gmail.com> ha scritto nel messaggio
news:1186923077.968478.215140@d55g2000hsg.googlegroups.com...

> Sorry about my short message yesterday, I was running out the door.

No problems, it happens Wink

>
> This article seems pretty straight forward:
>
> http://www.webforumz.com/php-forum/12595-multithreaded-php.htm
>

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-threads.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!
Back to top
petersprc

External


Since: Jul 02, 2007
Posts: 49



(Msg. 11) Posted: Sun Aug 12, 2007 7:18 pm
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

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);
}

?>

<h3>Launch</h3>

<? if ($done) { ?>
<p>Command launched in background.</p>
<? } ?>

<form action="launch.php">
<input type=submit name=btn value=" Go ">
</form>

On Aug 11, 12:47 pm, "Fabio" <znt.fa... DeleteThis @virgilio.it> wrote:
> 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
Back to top
gosha bine

External


Since: Apr 27, 2007
Posts: 207



(Msg. 12) Posted: Mon Aug 13, 2007 9:51 am
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

On 11.08.2007 21:41 Fabio wrote:
> "ELINTPimp" <smsiebe.DeleteThis@gmail.com> ha scritto nel messaggio
> 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 Smile
>
> 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 Wink http://code.google.com/p/pihipi
Back to top
ELINTPimp

External


Since: Jun 30, 2007
Posts: 42



(Msg. 13) Posted: Mon Aug 13, 2007 12:33 pm
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

On Aug 13, 3:51 am, gosha bine <stereof....RemoveThis@gmail.com> wrote:
> On 11.08.2007 21:41 Fabio wrote:
>
>
>
> > "ELINTPimp" <smsi....RemoveThis@gmail.com> ha scritto nel messaggio
> >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 Smile
>
> > 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.
Back to top
Toby A Inkster

External


Since: Jul 17, 2007
Posts: 111



(Msg. 14) Posted: Tue Aug 14, 2007 5:53 pm
Post subject: Re: Asynchronous function call?
Archived from groups: per prev. post (more info?)

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/
Back to top
Display posts from previous:   
       Webmaster Forums (Home) -> PHP
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum