By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,636 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

Asynchronous function call?

P: n/a
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
Aug 11 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On Aug 11, 12:47 pm, "Fabio" <znt.fa...@virgilio.itwrote:
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."

Aug 11 '07 #2

P: n/a
On Aug 11, 11:47 am, "Fabio" <znt.fa...@virgilio.itwrote:
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).

Aug 11 '07 #3

P: n/a
"ELINTPimp" <sm*****@gmail.comha scritto nel messaggio
news:11**********************@k79g2000hse.googlegr oups.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.
Aug 11 '07 #4

P: n/a
On Aug 11, 2:41 pm, "Fabio" <znt.fa...@virgilio.itwrote:
"ELINTPimp" <smsi...@gmail.comha scritto nel messaggionews:11**********************@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.
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.
Aug 11 '07 #5

P: n/a
C.
On 11 Aug, 20:41, "Fabio" <znt.fa...@virgilio.itwrote:
>
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.
Aug 11 '07 #6

P: n/a
"ELINTPimp" <sm*****@gmail.comha scritto nel messaggio
news:11*********************@57g2000hsv.googlegrou ps.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
Aug 11 '07 #7

P: n/a

"C." <co************@gmail.comha scritto nel messaggio
news:11*********************@19g2000hsx.googlegrou ps.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... :(

Aug 11 '07 #8

P: n/a
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...@virgilio.itwrote:
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

Aug 12 '07 #9

P: n/a
On 11.08.2007 21:41 Fabio wrote:
"ELINTPimp" <sm*****@gmail.comha scritto nel messaggio
news:11**********************@k79g2000hse.googlegr oups.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
Aug 13 '07 #10

P: n/a
On Aug 13, 3:51 am, gosha bine <stereof...@gmail.comwrote:
On 11.08.2007 21:41 Fabio wrote:
"ELINTPimp" <smsi...@gmail.comha scritto nel messaggio
news:11**********************@k79g2000hse.googlegr oups.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.

Aug 13 '07 #11

P: n/a
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/
Aug 14 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.