我有一个包含100个xlxs文件的目录。现在,我想要做的是将所有这些文件转换成PDF格式,所有这些文件都是一次性的,或者是一次性的。转换过程目前正在与foreach和cron很好地工作。但是它可以一次一个地处理或转换文件,这会增加等待PDF文件的用户端的等待时间。
此时我正在考虑并行处理,但不知道如何实现。
这是我目前的代码
$files = glob("/var/www/html/conversions/xlxs_files/*");
if(!empty($files)){
$now = time();
$i = 1;
foreach ($files as $file) {
if (is_file($file) && $i <= 8) {
echo $i.'-----'.basename($file).'----'.date('m/d/Y H:i:s',@filemtime($file));
echo '<br>';
$path_parts = pathinfo(basename($file));
$xlsx_file_name = basename($file);
$pdf_file_name = $path_parts['filename'].'.pdf';
echo '<br>';
try{
$result = ConvertApi::convert('pdf', ['File' => $common_path.'xlxs_files/'.$xlsx_file_name],'xlsx');
echo $log = 'conversion start for '.basename($file).' on '. date('d-M-Y h:i:s');
echo '<br>';
$result->getFile()->save($common_path.'pdf_files/'.$pdf_file_name);
echo $log = 'conversion start for '.basename($file).' on '. date('d-M-Y h:i:s');
echo '<br>';
mail('amit.webethics@gmail.com','test','test');
unlink($common_path.'xlxs_files/'.$xlsx_file_name);
}catch(Exception $e){
$log_file_data = createAlogFile();
$log = 'There is an error with your file .'. $xlsx_file_name.' -- '.$e->getMessage();
file_put_contents($log_file_data, $log . "\n", FILE_APPEND);
continue;
}
$i++;
}
}
}else{
echo 'nothing to process';
}
任何帮助都将不胜感激。谢谢
发布于 2020-09-21 05:56:52
您可以一次启动多个PHP脚本。如何做到这一点,详细的答案是:https://unix.stackexchange.com/a/216475/91593,我会选择这个解决方案:
N=4
(
for thing in a b c d e f g; do
((i=i%N)); ((i++==0)) && wait
task "$thing" &
done
)
另一种方法是尝试使用PHP。对于这个问题有一个深入的答案:https://stackoverflow.com/a/36440644/625521
https://stackoverflow.com/questions/63986557
复制相似问题