php长脚本,长PHP脚本运行多次
你有:
> max_execution_time已禁用.只要需要,您的腳本將不會在該過程完成之前結束.
> memory_limit已禁用.內存中存儲的數據量沒有限制.
完成500條記錄沒有問題.這表明腳本在下一次cronjob迭代之前完成其過程.例如,如果您的cron每小時運行一次,則會在不到一小時的時間內處理500條記錄.
如果您有一個將要處理大量記錄的cronjob,那么請考慮向該進程添加鎖定機制.僅允許腳本運行一次,并在上一個過程完成時重新啟動.
在執行php腳本之前,您可以將腳本鎖創建為shell腳本的一部分.或者,如果您無法訪問服務器,則可以在php腳本中使用數據庫鎖定,如下所示.
class ProductCronJob
{
protected $lockValue;
public function run()
{
// Obtain a lock
if ($this->obtainLock()) {
// Run your script if you have valid lock
$this->syncProducts();
// Release the lock on complete
$this->releaseLock();
}
}
protected function syncProducts()
{
// your long running script
}
protected function obtainLock()
{
$time = new \DateTime;
$timestamp = $time->getTimestamp();
$this->lockValue = $timestamp . '_syncProducts';
$db = JFactory::getDbo();
$lock = [
'lock' => $this->lockValue,
'timemodified' => $timestamp
];
// lock = '0' indicate that the cronjob is not active.
// Update #__cronlock set lock = '', timemodified = '' where name = 'syncProducts' and lock = '0'
// $result = $db->updateObject('#__cronlock', $lock, 'id');
// $lock = SELECT * FROM #__cronlock where name = 'syncProducts';
if ($lock !== false && (string)$lock !== (string)$this->lockValue) {
// Currently there is an active process - can't start a new one
return false;
// You can return false as above or add extra logic as below
// Check the current lock age - how long its been running for
// $diff = $timestamp - $lock['timemodified'];
// if ($diff >= 25200) {
// // The current script is active for 7 hours.
// // You can change 25200 to any number of seconds you want.
// // Here you can send notification email to site administrator.
// // ...
// }
}
return true;
}
protected function releaseLock()
{
// Update #__cronlock set lock = '0' where name = 'syncProducts'
}
}
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的php长脚本,长PHP脚本运行多次的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab计算函数区域面积,matla
- 下一篇: image.helper.php,ima