I'm using zend_mail from zend-framework2 in my project to send some amount of emails in a loop, but sometimes it takes more than usual to send an email.
After doing some research, i found that the delay occurs on the stream_socket_client function.
I tried to set this function's timeout to acceptable value, but it seems to ignore that setting.
Also tried to use STREAM_CLIENT_PERSISTENT to limit the number of opened sockets but with no luck.
Average stream_socket_client times are about 0.03 seconds and occasionally it takes from 5 seconds up to even 40 seconds. Every value above 0.5 seconds is unacceptable for me. I'm out of ideas what can cause that issue.
Current setup:
$start = microtime(true);
$this->socket = #stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT);
echo 'Stream socket: '.(microtime(true) - $start);
Related
I am using gethostbyname function to get IP address for our domain names.
When its running in apache and also using php in command line its taking 5.0695459842682 seconds for complete result.
<?php
$domain_name = $argv[1];
$stime = microtime(true);
$ip =gethostbyname($domain_name);
$etime = microtime(true);
$ttime = $etime - $stime;
echo "Total time for gethostbyname : $ttime\n";
echo $ip."\n";
?>
When I am running above script in php command line by passing google.com as domain, Its returning below result.
Total time for gethostbyname : 5.0695459842682
216.58.203.142
Can anyone please help me to come out and reduce the time to less than 1 second.
Regards,
Vignesh Kumar K
Maybe the problem is that your system is trying connect to dns server using ipv6 but your network is configured not properly to finish this with success? If so, you can try to disable ipv6.
In Debian you could do that just like here: https://wiki.debian.org/DebianIPv6#How_to_turn_off_IPv6
It depends on the internet speed you are using. When I run the code i am getting less than 2 seconds as my internet speed is nearly 90Mbps.
Hence you cant do anything to optimize the code rather than you need to increase your internet speed.
I need to get the current(!) milliseconds for a custom timer-implmentation, but the milliseconds have to be independent from the actual system date/time:
When ever the system date/time is set to a new value (e.g. NTP-Update), the running(!) script should not be affected:
$t=microtime(true);
while ( ($t+100) < microtime(true) ) {
...
}
This loop is just waiting for 100 seconds. But what happens, when the user (or system) sets a new system date/time while the loop is running? Then the code would run into problems, because microtime() gives an absolute value - not a relative amount of milliseconds.
What I need is a (system-wide) function that tells me a system-date/time-independent value of milliseconds, e.g. milliseconds since boot-up.
Any idea?
I'm writing a a php script where i do a loop and inside the loop there is a particular function processing information, as illustrated here.
while (some condition){
// some code
processInformation();
// Some more code
}
Well it turns out that midway, inside processInformation(), I encountered the error Fatal error: Maximum execution time of 30 seconds exceeded. Using set_time_limit(0), would not help in my situation because processInformation() might take forever. I don't want an error to completely break my program, so is there anyway i can catch an exception or tell php to "continue" the loop if processInformation() takes too long?
You can increase the value of maximum execution time from 30 seconds to any value. For example: if you want to set the limit of maximum execution time of 3 minutes (instead of 30 seconds), then you can set as:
ini_set('max_execution_time', 180); //maximum execution time of 3 minutes
This way, you can set the time based on your need.
I dunno how to catch the exception but you could use a simple time calculation to escape after say 60 secs. ()
$x=time();
while (some condition){
processInformation();
if ($x + 60) < time()
break;
}
Say my set_time_limit is set to 30 seconds and can't be changed. I have an email script in php that will run longer than 30 seconds on 1 cron job (I haven't reached that time mark yet.) What happens on timeout after 30 seconds? Will the script stop then continue?
If not, I would like to record the elapsed time from the beginning of my script's loop until it reaches 30 seconds and pause the process then continue.
What is a good way to do this?
Update: what I think might work
function email()
{
sleep(2); //delays script 2 seconds (time to break script on reset)
foreach ($emails as $email):
// send individual emails with different content
// takes longer than 30 seconds
enforeach;
// on 28 seconds
return email(); //restarts process
}
Suggested approach:
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
foreach ($emails as $email):
// send individual emails with different content
// takes longer than 30 seconds
$time_curr = microtime_float();
$time = $time_curr - $time_start;
if($time > 28){ //if time is bigger than 28 seconds (2 seconds less - just in case)
break;// we will continue processing the rest of the emails - next time cron will call the script
}
enforeach;
What you ask is not possible. When the script timeout is reached, it stops. You cannot pause it and continue it. You can only restart it. Let me tell you what I did to solve a similar problem.
Part One:
Queue all your emails into a simple database. (Just a few fields like to,
from, subject, message, and a flag to track if it is sent.)
Part Two.
1. Start script
2. Set a variable with the start time
3. Check the db for any mails that have not been sent yet
4. If you find any start a FOR loop
5. Check if more than 20 seconds have elapsed (adjust the window here, I like
to allow some spare time in case a mail call takes a few seconds longer.)
6. If too much time passed, exit the loop, go to step 10
7. Send an email
8. Mark this email as sent in the db
9. The FOR loop takes us back to step 4
10. Exit the script
Run this every 60 seconds. It sends what it can, then the rest gets sent next time.
Is it time()?
Is it time().substr(microtime(), 2, 2)?
Is it time().substr(microtime(), 2, 3)?
Kind of lost with the following snippet.
function updateClock ( ) {
var timeStamp = <?php echo time().substr(microtime(), 2, 2);?>;
var currentTime = new Date ( );
currentTime.setTime( timeStamp );
...
...
}
My goal is to use server time and start ticking from there on client browser window. The code above either returns the current client computer time or sometime in 1973. I guess I'm not getting the right time stamp format for setTime()?
Thanks!
1000
I tried that but the web page still shows my local time after I upload the js.php (rendering the javascript code) to my server. My server is approx 12 hours different in time from me. My guess is that does php takes client side time into account running time() ? I mean browsers do send request time to apache right?
I copied the time() * 1000 returned value from the web page run on my server and pasted it into a local page:
<script type="text/javascript">
var d = new Date();
d.setTime(1233760568000);
document.write(d);
</script>
And it's indeed my local time. Thus the guess.
Is there anyway to specify time zone for time()?
Date.setTime expects the number of milliseconds since 1970-1-1. php's time function yields the number of seconds since 1970-1-1. Therefore, you can just use
var TimeStamp = <?php echo time()*1000;?>
Due to latency issues (the browsers needs to load the whole page before starting JavaScript), the time will usually drift one or a couple of seconds though.
Multiply by 1000. JavaScript expects milliseconds while PHP returns seconds.
Date.setTime() wants milliseconds since the Unix Epoch, and time() returns seconds since then. If absolute precision isn't required (and given your methodology, I don't think it is), just multiply the value you get from time() by 1000.
Edit: beaten twice--D'oh