FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » time consuming loop
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
time consuming loop [message #170865] Sat, 04 December 2010 07:39 Go to next message
paulfredlein is currently offline  paulfredlein
Messages: 8
Registered: December 2010
Karma: 0
Junior Member
Hi,

If I use a loop that's too time consuming the browser will complain. Is
there a callback or threads or something I can use and only do a small
number each time instead of the whole lot at once?

for(short i = 0; i < LONG_INT; i++)
{
doSomething();
}

Thanks,

Paul
Re: time consuming loop [message #170873 is a reply to message #170865] Sat, 04 December 2010 12:51 Go to previous messageGo to next message
Kim Andr Aker is currently offline  Kim Andr Aker
Messages: 17
Registered: September 2010
Karma: 0
Junior Member
På Sat, 04 Dec 2010 08:39:26 +0100, skrev Paul Fredlein
<paulfredlein(at)optusnet(dot)com(dot)au>:

> Hi,
>
> If I use a loop that's too time consuming the browser will complain. Is
> there a callback or threads or something I can use and only do a small
> number each time instead of the whole lot at once?
>
> for(short i = 0; i < LONG_INT; i++)
> {
> doSomething();
> }

Maybe it's not the browser complaining (at least not on its own) - I would
think your script runs into the maximum script execution time (by default
set to 30 seconds, but may differ depending on the host configuration).

One way to work around this, is to reset the counter after each loop using
set_time_limit().
http://php.net/set-time-limit

Like this:


for(short i = 0; i < LONG_INT; i++)
{
doSomething();
set_time_limit(30);
}

--
Kim André Akerø
- kimandre(at)NOSPAMbetadome(dot)com
(remove NOSPAM to contact me directly)
Re: time consuming loop [message #170874 is a reply to message #170865] Sat, 04 December 2010 13:37 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On Dec 4, 7:39 am, paulfredl...@optusnet.com.au (Paul Fredlein) wrote:
> Hi,
>
> If I use a loop that's too time consuming the browser will complain. Is
> there a callback or threads or something I can use and only do a small
> number each time instead of the whole lot at once?
>
> for(short i = 0; i < LONG_INT; i++)
> {
>      doSomething();
>
> }
>
> Thanks,
>
> Paul

Your browser knows nothing of what php is doing.

PHP however will only execute valid php, which the code you have
posted does not appear to be.
Re: time consuming loop [message #170876 is a reply to message #170865] Sat, 04 December 2010 14:02 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 12/4/2010 2:39 AM, Paul Fredlein wrote:
> Hi,
>
> If I use a loop that's too time consuming the browser will complain. Is
> there a callback or threads or something I can use and only do a small
> number each time instead of the whole lot at once?
>
> for(short i = 0; i< LONG_INT; i++)
> {
> doSomething();
> }
>
> Thanks,
>
> Paul

Not in PHP. The browser will time out after whatever setting the
browser has. And this won't be until the PHP code on the server has
finished.

You could get around this with AJAX (see comp.lang.javascript), but the
question is - what are you doing which takes this long? People are not
going to wait very long for a web page to load. And if the browser is
timing out, that's way too long.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: time consuming loop [message #170881 is a reply to message #170865] Sat, 04 December 2010 17:50 Go to previous messageGo to next message
Twayne is currently offline  Twayne
Messages: 135
Registered: September 2010
Karma: 0
Senior Member
In news:1jszks5.1v6hzcuelpu9sN%paulfredlein(at)optusnet(dot)com(dot)au,
Paul Fredlein <paulfredlein(at)optusnet(dot)com(dot)au> typed:
> Hi,
>
> If I use a loop that's too time consuming the browser will
> complain. Is there a callback or threads or something I can
> use and only do a small number each time instead of the
> whole lot at once?
>
> for(short i = 0; i < LONG_INT; i++)
> {
> doSomething();
> }
>
> Thanks,
>
> Paul

Perhaps you should describe what you're trying to do. I know if I ran into a
web page that timed out I'd probably already be long gone and not return.
More info would help.
HTH,

Twayne`
Re: time consuming loop [message #170884 is a reply to message #170881] Sun, 05 December 2010 00:56 Go to previous messageGo to next message
paulfredlein is currently offline  paulfredlein
Messages: 8
Registered: December 2010
Karma: 0
Junior Member
Hi,

I'm trying to automate the emailing of my newsletter to my contact
group.

In php, I'm thinking of using an arrary of email addresses derived from
a mysql contact list. Then looping through the array emailing each item
with my newsletter.

Originally, I played with this inside a mysql query and delaying each
email by a second so that the host server doesn't think I'm a spammer.
But then the calling php inside my browser just waited and became
'unresponsive'.

So then I thought of the array of email addresses and wondered if
there's a thread or callback mechanism I can use. Or perhaps I can keep
track of a progress variable and use a smaller loop initializing a loop
with the progress variable which is the index into the array - or
something.

To send the newsletter, I just open that php page in my browser and
enter a password and away it goes.

I'm sure this is not the proper way to do this but it's only for my use
and not a commercial product or anythng like that.

Thanks,

Paul


Twayne <nobody(at)devnull(dot)spamcop(dot)net> wrote:

> In news:1jszks5.1v6hzcuelpu9sN%paulfredlein(at)optusnet(dot)com(dot)au,
> Paul Fredlein <paulfredlein(at)optusnet(dot)com(dot)au> typed:
>> Hi,
>>
>> If I use a loop that's too time consuming the browser will
>> complain. Is there a callback or threads or something I can
>> use and only do a small number each time instead of the
>> whole lot at once?
>>
>> for(short i = 0; i < LONG_INT; i++)
>> {
>> doSomething();
>> }
>>
>> Thanks,
>>
>> Paul
>
> Perhaps you should describe what you're trying to do. I know if I ran into a
> web page that timed out I'd probably already be long gone and not return.
> More info would help.
> HTH,
>
> Twayne`
Re: time consuming loop [message #170885 is a reply to message #170884] Sun, 05 December 2010 13:06 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Paul Fredlein wrote:
> Hi,
>
> I'm trying to automate the emailing of my newsletter to my contact
> group.
>
> In php, I'm thinking of using an arrary of email addresses derived from
> a mysql contact list. Then looping through the array emailing each item
> with my newsletter.
>
> Originally, I played with this inside a mysql query and delaying each
> email by a second so that the host server doesn't think I'm a spammer.
> But then the calling php inside my browser just waited and became
> 'unresponsive'.
>
> So then I thought of the array of email addresses and wondered if
> there's a thread or callback mechanism I can use. Or perhaps I can keep
> track of a progress variable and use a smaller loop initializing a loop
> with the progress variable which is the index into the array - or
> something.
>
> To send the newsletter, I just open that php page in my browser and
> enter a password and away it goes.
>
> I'm sure this is not the proper way to do this but it's only for my use
> and not a commercial product or anythng like that.
>
> Thanks,
>
> Paul
>

I just built this for a small charity site.

Why not simply send the one email Bcc:'ed to all the recipients?


That will be queued in milliseconds, and go when the mailer gets around
to it.

My ISP is happy to let me do that. I asked them.
Re: time consuming loop [message #170886 is a reply to message #170885] Sun, 05 December 2010 14:00 Go to previous messageGo to next message
paulfredlein is currently offline  paulfredlein
Messages: 8
Registered: December 2010
Karma: 0
Junior Member
Hi,

I never thought of Bcc, but I have around 300 to email.

Regards,

Paul


The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:

> Paul Fredlein wrote:
>> Hi,
>>
>> I'm trying to automate the emailing of my newsletter to my contact
>> group.
>>
>> In php, I'm thinking of using an arrary of email addresses derived from
>> a mysql contact list. Then looping through the array emailing each item
>> with my newsletter.
>>
>> Originally, I played with this inside a mysql query and delaying each
>> email by a second so that the host server doesn't think I'm a spammer.
>> But then the calling php inside my browser just waited and became
>> 'unresponsive'.
>>
>> So then I thought of the array of email addresses and wondered if
>> there's a thread or callback mechanism I can use. Or perhaps I can keep
>> track of a progress variable and use a smaller loop initializing a loop
>> with the progress variable which is the index into the array - or
>> something.
>>
>> To send the newsletter, I just open that php page in my browser and
>> enter a password and away it goes.
>>
>> I'm sure this is not the proper way to do this but it's only for my use
>> and not a commercial product or anythng like that.
>>
>> Thanks,
>>
>> Paul
>>
>
> I just built this for a small charity site.
>
> Why not simply send the one email Bcc:'ed to all the recipients?
>
>
> That will be queued in milliseconds, and go when the mailer gets around
> to it.
>
> My ISP is happy to let me do that. I asked them.
Re: time consuming loop [message #170891 is a reply to message #170886] Sun, 05 December 2010 17:59 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Paul Fredlein wrote:
> Hi,
>
> I never thought of Bcc, but I have around 300 to email.
>
Ask your ISP.

Mine said up to 250 or 250 an hour, 'but we can make an exception for
your IP address' or something. I have no more than 100 at the moment


here's my code - adjusted for anonymity
$employee _id contains the identity of the person using the mailer -
they are uses as the return and fro addresses.

$_POST['status'] selects a subset of the entire mailing list.

function sendmail()
{
global $employee_id;
if(isset($_POST['status']))
$status=$_POST['status'];
else return("-1");
if (strlen($_POST['body'])<1)
return ("-2");
else $body=stripslashes($_POST['body']);
$subject=$_POST['subject'];
$return_path="webmaster(at)mysite(dot)org";
$from=mysql_result(mysql_query(sprintf("select email
from people
where id='%d'",
$employee_id)),0,'email');
// get list of addressees
mysql_query("set group_concat_max_len = 8192");
//...to avoid truncation
// This bit gets a comma delimited array of email
// addresses disregarding those that have no '@' in them
$email=mysql_result(mysql_query(sprintf("select 1 as g,
group_concat(email separator ',') as them
from people
where status>='%d' and instr(email,'@')
group by g",$status)),
0,'them');
//
$headers = sprintf("From: %s\nBcc:%s\n", $from,$email);
mail('', $subject, $body, $headers, "-f ".$return_path );
return("");
}


That may serve as a starting point for you.
Re: time consuming loop [message #170894 is a reply to message #170891] Sun, 05 December 2010 18:35 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On Dec 5, 5:59 pm, The Natural Philosopher <t...@invalid.invalid>
wrote:
> Paul Fredlein wrote:
>> Hi,
>
>> I never thought of Bcc, but I have around 300 to email.
>
> Ask your ISP.
>
> Mine said up to 250 or 250 an hour,
Can you explain that?
Re: time consuming loop [message #170898 is a reply to message #170891] Mon, 06 December 2010 07:58 Go to previous messageGo to next message
paulfredlein is currently offline  paulfredlein
Messages: 8
Registered: December 2010
Karma: 0
Junior Member
Hi,

Thanks for that. I'll have a play and see what happens.

Regards,

Paul


The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:

> Paul Fredlein wrote:
>> Hi,
>>
>> I never thought of Bcc, but I have around 300 to email.
>>
> Ask your ISP.
>
> Mine said up to 250 or 250 an hour, 'but we can make an exception for
> your IP address' or something. I have no more than 100 at the moment
>
>
> here's my code - adjusted for anonymity
> $employee _id contains the identity of the person using the mailer -
> they are uses as the return and fro addresses.
>
> $_POST['status'] selects a subset of the entire mailing list.
>
> function sendmail()
> {
> global $employee_id;
> if(isset($_POST['status']))
> $status=$_POST['status'];
> else return("-1");
> if (strlen($_POST['body'])<1)
> return ("-2");
> else $body=stripslashes($_POST['body']);
> $subject=$_POST['subject'];
> $return_path="webmaster(at)mysite(dot)org";
> $from=mysql_result(mysql_query(sprintf("select email
> from people
> where id='%d'",
> $employee_id)),0,'email');
> // get list of addressees
> mysql_query("set group_concat_max_len = 8192");
> //...to avoid truncation
> // This bit gets a comma delimited array of email
> // addresses disregarding those that have no '@' in them
> $email=mysql_result(mysql_query(sprintf("select 1 as g,
> group_concat(email separator ',') as them
> from people
> where status>='%d' and instr(email,'@')
> group by g",$status)),
> 0,'them');
> //
> $headers = sprintf("From: %s\nBcc:%s\n", $from,$email);
> mail('', $subject, $body, $headers, "-f ".$return_path );
> return("");
> }
>
>
> That may serve as a starting point for you.
Re: time consuming loop [message #170904 is a reply to message #170894] Mon, 06 December 2010 22:09 Go to previous message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Captain Paralytic wrote:
> On Dec 5, 5:59 pm, The Natural Philosopher <t...@invalid.invalid>
> wrote:
>> Paul Fredlein wrote:
>>> Hi,
>>> I never thought of Bcc, but I have around 300 to email.
>> Ask your ISP.
>>
>> Mine said up to 250 or 250 an hour,
> Can you explain that?

Up to 250 addressees in a single message, or up to 250 separate emails
per hour. Or whatever the worst combination was of the two. I think they
just count the total addressee list irrsepectve of number of emails and
if it goes over 250 in a hour, Robert is a relative and its bouncy
castle time...
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Demo PHP/Ajax slideshow server crashing
Next Topic: Apache + PHP module segfaults
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 12:42:27 GMT 2024

Total time taken to generate the page: 0.02995 seconds