Oguzz13 Posted January 14, 2013 Share Posted January 14, 2013 (edited) Hi, In the below code, each PdfGen2, 3, 4.. function starts only after the previous one ends.. And the result of the previous function comes in 3-4 seconds (it posts some info to a plugin and receives some info back), so it delays a lot when it comes to the 7th or 8th function. How can I make the functions to start at the same time and doesn't require to wait each other to finish their own functions. #Explanation.. This is where the code checks if there is more than 1 pdf. If there is, it sends it to different functions (PdfGen2, PdfGen3..) but the problem is it waits to previous function to be finished and return the necessary stamped new pdf's url. The new function only works after that and each time it takes 2-3 seconds to do that. if ($tarih_sql == true) { $urun_id = $db->VeriCek($Id, 'siparisler', 'urun_id'); $pdf_url = $ykd->PdfGen($Id); $urun_pdf_url2 = $db->VeriCek($urun_id, 'urunler', 'pdf_url2'); if ($urun_pdf_url2 != '') { $pdf_url2 = $ykd->PdfGen2($Id, '0', '2'); $pdf_url_sql .= ", pdf_url2='$pdf_url2'"; } $urun_pdf_url3 = $db->VeriCek($urun_id, 'urunler', 'pdf_url3'); if ($urun_pdf_url3 != '') { $pdf_url3 = $ykd->PdfGen3($Id, '0', '3'); $pdf_url_sql .= ", pdf_url3='$pdf_url3'"; } $urun_pdf_url4 = $db->VeriCek($urun_id, 'urunler', 'pdf_url4'); if ($urun_pdf_url4 != '') { $pdf_url4 = $ykd->PdfGen4($Id, '0', '4'); $pdf_url_sql .= ", pdf_url4='$pdf_url4'"; } $urun_pdf_url5 = $db->VeriCek($urun_id, 'urunler', 'pdf_url5'); if ($urun_pdf_url5 != '') { $pdf_url5 = $ykd->PdfGen5($Id, '0', '5'); $pdf_url_sql .= ", pdf_url5='$pdf_url5'"; } $urun_pdf_url6 = $db->VeriCek($urun_id, 'urunler', 'pdf_url6'); if ($urun_pdf_url6 != '') { $pdf_url6 = $ykd->PdfGen6($Id, '0', '6'); $pdf_url_sql .= ", pdf_url6='$pdf_url6'"; } $urun_pdf_url7 = $db->VeriCek($urun_id, 'urunler', 'pdf_url7'); if ($urun_pdf_url7 != '') { $pdf_url7 = $ykd->PdfGen7($Id, '0', '7'); $pdf_url_sql .= ", pdf_url7='$pdf_url7'"; } $urun_pdf_url8 = $db->VeriCek($urun_id, 'urunler', 'pdf_url8'); if ($urun_pdf_url8 != '') { $pdf_url8 = $ykd->PdfGen8($Id, '0', '8'); $pdf_url_sql .= ", pdf_url8='$pdf_url8'"; } $urun_pdf_url9 = $db->VeriCek($urun_id, 'urunler', 'pdf_url9'); if ($urun_pdf_url9 != '') { $pdf_url9 = $ykd->PdfGen9($Id, '0', '9'); $pdf_url_sql .= ", pdf_url9='$pdf_url9'"; } $urun_pdf_url10 = $db->VeriCek($urun_id, 'urunler', 'pdf_url10'); if ($urun_pdf_url10 != '') { $pdf_url10 = $ykd->PdfGen10($Id, '0', '10'); $pdf_url_sql .= ", pdf_url10='$pdf_url10'"; } $urun_pdf_url11 = $db->VeriCek($urun_id, 'urunler', 'pdf_url11'); if ($urun_pdf_url11 != '') { $pdf_url11 = $ykd->PdfGen11($Id, '0', '11'); $pdf_url_sql .= ", pdf_url11='$pdf_url11'"; } $urun_pdf_url12 = $db->VeriCek($urun_id, 'urunler', 'pdf_url12'); if ($urun_pdf_url12 != '') { $pdf_url12 = $ykd->PdfGen12($Id, '0', '12'); $pdf_url_sql .= ", pdf_url12='$pdf_url12'"; } $urun_pdf_url13 = $db->VeriCek($urun_id, 'urunler', 'pdf_url13'); if ($urun_pdf_url13 != '') { $pdf_url13 = $ykd->PdfGen13($Id, '0', '13'); $pdf_url_sql .= ", pdf_url13='$pdf_url13'"; } $guncelle = $db->Sorgu("Update siparisler Set pdf_url='$pdf_url' $pdf_url_sql Where id='$Id' Limit 1"); $eposta = $db->VeriCek($Id, 'siparisler', 'eposta'); $essiz_id = $db->VeriCek($Id, 'siparisler', 'essiz_id'); $urun_adi = $db->VeriCek($urun_id, 'urunler', 'baslik'); $destek_mail = $db->VeriCek($urun_id, 'urunler', 'destek_mail'); $indir_link = _SiteDomain_.'indir/'.$Id.'/'.$essiz_id.'/'; $mail_kime = $eposta; $mail_kimden = $destek_mail; $mail_konu = 'İndirme Bağlantınız'; $mail_mesaj = ' Alttaki Bağlantıya Tıklayıp Dosyanızı İndirebilirsiniz.<br /><br /> Bağlantı:<br /> <a href="'.$indir_link.'">'.$indir_link.'</a> <br /><br /> Bize '.$destek_mail.' mail adresinden ulaşabilirsiniz. <br /><br /> <b>'.$urun_adi.' Yönetimi</b> '; $mail->Gonder($mail_kime, $mail_konu, $mail_mesaj, $mail_kimden); } Edited January 14, 2013 by Oguzz13 Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/ Share on other sites More sharing options...
requinix Posted January 14, 2013 Share Posted January 14, 2013 PHP does not do asynchronous code. If you want to add that the complexity of this script will skyrocket. It looks like your database is very much not normalized. Are you able to change the schema? Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405623 Share on other sites More sharing options...
BagoZonde Posted January 14, 2013 Share Posted January 14, 2013 I will go for AJAX (and start to using variables' names in English language). Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405627 Share on other sites More sharing options...
Oguzz13 Posted January 14, 2013 Author Share Posted January 14, 2013 I've found sth named "parallel threading" and "async php".. as far as I've understood they make the function to run in the background. How can I change the code above, so that each function starts and runs in the background without waiting another one to end? Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405644 Share on other sites More sharing options...
premiso Posted January 14, 2013 Share Posted January 14, 2013 I've found sth named "parallel threading" and "async php".. as far as I've understood they make the function to run in the background. How can I change the code above, so that each function starts and runs in the background without waiting another one to end? Read up on how to do it and then implement it! Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405645 Share on other sites More sharing options...
Oguzz13 Posted January 14, 2013 Author Share Posted January 14, 2013 PHP does not do asynchronous code. If you want to add that the complexity of this script will skyrocket. It looks like your database is very much not normalized. Are you able to change the schema? I cut and pasted the code sample, that because of that I think.. Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405646 Share on other sites More sharing options...
Oguzz13 Posted January 14, 2013 Author Share Posted January 14, 2013 Read up on how to do it and then implement it! I'm not Guru like you on PHP. I need some help if you don't mind. Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405651 Share on other sites More sharing options...
requinix Posted January 14, 2013 Share Posted January 14, 2013 I've found sth named "parallel threading" and "async php".. as far as I've understood they make the function to run in the background. How can I change the code above, so that each function starts and runs in the background without waiting another one to end? Functions do not run in the background. You can run an entire script in the background but then you have to manage the various instances of the scripts and get them to communicate with each other. Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405652 Share on other sites More sharing options...
requinix Posted January 14, 2013 Share Posted January 14, 2013 In order of importance, 1. You really, really need to decide if the running time is a problem. Is this something that can run via cron instead? Can you send the email first and do the updates second? 2. Why are there 13 fields for the "pdf_url"? That screams of a poor database design and I bet you fixing that would eliminate the time problem. 3. Is this script running from a web page or the command line? It dictates what method(s) to use to run a script in the background. 4. Can the 13 potential updates be run independently of each other? Seems like it. 5. Assuming $db->VeriCek() does nothing more complicated than a SELECT you can probably run all these updates, either together or individually, with a single query. No PHP code required. Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405658 Share on other sites More sharing options...
Oguzz13 Posted January 14, 2013 Author Share Posted January 14, 2013 This is a part of my shopping card written in PHP which is designed to sell pdf's. And there is a wordpress plugin installed in another site, which stamps pdf's with customer details. The shopping card posts the customer info's to the plug-in for each pdf. In the "if statements" above it checks whether there is another pdf file or not. We sell multiple pdf's together. Althuogh all pdf's are sent to different plug-in's and has nothing to do with order, because of the code above, it waits the first pdf to be stamped (about 3-4 seconds) in the first plug-in and then moves to the next one. I need to stamp at least 8 pdf's like this and it makes about 25-30 seconds after a purchase, which is a little bit long. That's why I want to make these functions to work at the same time, so that all the pdf info's can be send to the plug-in's simultanously, and be stamped at total 3-4 seconds.. Instead of 25-30 seconds.. Because customer is redirected to the download page after a succesfull purchase.. 25-30 seconds waiting is a little long for a credit card purchase. The system runs in a Linux server and we can make necessary changes if there are any. Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405668 Share on other sites More sharing options...
Oguzz13 Posted January 14, 2013 Author Share Posted January 14, 2013 I've opened a freelancer job in the forum about this issue. If you believe that you can convert this code to work without waiting each other (implementing multi-threading or any other way..), please free to contact with me via PM or oguzhrsn@gmail.com Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405707 Share on other sites More sharing options...
The Letter E Posted January 15, 2013 Share Posted January 15, 2013 pcntl_fork() is an option for forking your php, however it's not usually compiled with most installs, so you'll have to make sure you can even use it. In the event you have access to pcntl_fork() you can in fact spawn an exact copy of the current process as a child process and run code in parallel. However, It's not recommended for use in a web server environment. Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405735 Share on other sites More sharing options...
trq Posted January 15, 2013 Share Posted January 15, 2013 You might also take a look at the react php framework. http://reactphp.org Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405748 Share on other sites More sharing options...
The Letter E Posted January 15, 2013 Share Posted January 15, 2013 You might also take a look at the react php framework. http://reactphp.org That looks promising. Thanks for the heads up. Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405750 Share on other sites More sharing options...
Oguzz13 Posted January 15, 2013 Author Share Posted January 15, 2013 pcntl_fork() is an option for forking your php, however it's not usually compiled with most installs, so you'll have to make sure you can even use it. In the event you have access to pcntl_fork() you can in fact spawn an exact copy of the current process as a child process and run code in parallel. However, It's not recommended for use in a web server environment. The server is our own VPS. We can make changes if possible. How can we be sure that the server runs that function or not? Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405766 Share on other sites More sharing options...
Oguzz13 Posted January 15, 2013 Author Share Posted January 15, 2013 You might also take a look at the react php framework. http://reactphp.org Is this a new library that needs to be added to PHP? Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405767 Share on other sites More sharing options...
trq Posted January 15, 2013 Share Posted January 15, 2013 The server is our own VPS. We can make changes if possible. How can we be sure that the server runs that function or not? As was already said, you shouldn't use pcntl_fork within a web environment. Is this a new library that needs to be added to PHP? It is a framework written in PHP for event driven, non blocking I/O with PHP. It's modeled in part on node.js. Quote Link to comment https://forums.phpfreaks.com/topic/273152-how-to-make-a-function-start-without-waiting-the-previous-one-to-end/#findComment-1405804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.