简体   繁体   English

在PHP中设置一段时间的睡眠时间

[英]setting Sleep time for a while loop in php

hey guys I'm trying to write an update script in which i need to set sleep time for each row and then continue the process 大家好,我试图编写一个更新脚本,在该脚本中,我需要为每一行设置睡眠时间,然后继续执行该过程

as long as the process is so giant and cannot be done at once ( switching Latin-1 Charset into UTF-8 ) then i need sleep time after each row is converted 只要该过程如此庞大且无法一次完成(将Latin-1字符集转换为UTF-8),那么每行转换后我都需要睡眠时间

this is a usual and working code to convert into utf-8 but the sleep time doesn't work and again its doing it all at once . 这是转换为utf-8的常用且有效的代码,但是睡眠时间无法正常工作,并且一次又做完了。

 ob_implicit_flush();

 $result = $db->sql_query("SHOW TABLES");
 while ($row = $db->sql_fetchrow($result)) {
  $db->sql_query("ALTER TABLE $row[0] COLLATE $collation");
  $result1 = $db->sql_query("SHOW COLUMNS FROM $row[0]");
  while ($row1 = $db->sql_fetchrow($result1)) {
   if (preg_match('~char|text|enum|set~', $row1["Type"])) {
    $db->sql_query("ALTER TABLE $row[0] MODIFY $row1[Field] $row1[Type] CHARACTER SET binary");
    $db->sql_query("ALTER TABLE $row[0] MODIFY $row1[Field] $row1[Type] COLLATE $collation" . ($row1["Null"] ? "" : " NOT NULL") . ($row1["Default"] && $row1["Default"] != "NULL" ? " DEFAULT '$row1[Default]'" : ""));
   }
   echo"sleep for 2 seconds ....<br> $row[0] is already converted.";  
 // wait for 2 seconds 
   usleep(2000000);
   // back!
   echo date('h:i:s') . "\n";

  }

 }

Have you tried simple sleep command 您是否尝试过简单的睡眠命令

sleep(2); 睡眠(2);

http://php.net/manual/en/function.sleep.php http://php.net/manual/zh/function.sleep.php

It seems that you are 'sleeping' between every table not between every column update - is that the way you wanted it to be? 似乎您在每个表之间而不是在每个列更新之间都在“睡眠”-这是您想要的方式吗? Anyway if data is big 2 seconds may be too low time 无论如何,如果数据很大2秒可能是太短的时间

There was also a bug in Windows PHP where you had to set infinite time limit or sleep would make high CPU usage. Windows PHP中还存在一个错误,您必须设置无限的时间限制,否则睡眠会占用大量CPU。

set_time_limit(0);

Unlike Simon, I can see that there is some merit in leaving gaps between the updates, however Why do you update each column in a seperate operation? 与Simon不同,我可以看到在更新之间留有一些优点,但是为什么要在单独的操作中更新每一列呢? It'll be a lot faster if you change them all at once. 如果一次全部更改,将会更快。

I do not see the effort of your sleep. 我看不到您的睡眠努力。 What are you waiting for during those two seconds? 在那两秒钟内,您还在等什么? The script can not do an other job while its sleeping. 该脚本在休眠时无法执行其他工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM