Aegidius Posted April 29, 2011 Share Posted April 29, 2011 If I had an heavy script, and I don't need to see the script result, can I execute that in background and let the user to continue vising the site, without waiting until the script is done? Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/ Share on other sites More sharing options...
JKG Posted April 29, 2011 Share Posted April 29, 2011 is this a stupid suggestion: place in an iframe at the bottom of page, with styling to display:none that way all the html would be processed the user would see the page, and the script, however long would be out of site out of mind... Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208288 Share on other sites More sharing options...
Aegidius Posted April 29, 2011 Author Share Posted April 29, 2011 Really thanks for the answer. That's a solution, but is there any other one more elegance? Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208292 Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 can I ask what the purpose of the script running in the background is? Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208295 Share on other sites More sharing options...
Aegidius Posted April 29, 2011 Author Share Posted April 29, 2011 I have an heavy script to be executed. If I put the code as main script the user will see a loading blank page for a while, until he can keep to navigate the site. I'd like that the main script throw a background script that execute the heave code, while the user can keep to navigate the site. Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208298 Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 but what does the "heavy code" do? Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208301 Share on other sites More sharing options...
Aegidius Posted April 29, 2011 Author Share Posted April 29, 2011 Database stuff and email stuff. Why? Is there no any way to throw a background script? Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208304 Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 you could launch a background process function launchBackgroundProcess($call) { // Windows if(is_windows()){ pclose(popen(’start /b ‘.$call.”, ‘r’)); } // Some sort of UNIX else { pclose(popen($call.‘ /dev/null &’, ‘r’)); } return true; } function is_windows(){ if(PHP_OS == ‘WINNT’ || PHP_OS == ‘WIN32′){ return true; } return false; } Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208311 Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 this is another method...which launches a new instance of the PHP interpreter as a backgounrd process.. exec ("/usr/bin/php path/to/script.php >/dev/null &"); Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208314 Share on other sites More sharing options...
Aegidius Posted April 29, 2011 Author Share Posted April 29, 2011 Ok, really thanks. I'll try. Do you know if there is any way to throw an asynchronous script like you do in ajax? Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208316 Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 this function will submit an asynchronous POST query to the specified url function backgroundPost($url){ $parts=parse_url($url); $fp = fsockopen($parts['host'], isset($parts['port'])?$parts['port']:80, $errno, $errstr, 30); if (!$fp) { return false; } else { $out = "POST ".$parts['path']." HTTP/1.1\r\n"; $out.= "Host: ".$parts['host']."\r\n"; $out.= "Content-Type: application/x-www-form-urlencoded\r\n"; $out.= "Content-Length: ".strlen($parts['query'])."\r\n"; $out.= "Connection: Close\r\n\r\n"; if (isset($parts['query'])) $out.= $parts['query']; fwrite($fp, $out); fclose($fp); return true; } } //Example of use backgroundPost('http://example.com/slow.php?file='. urlencode('some file.dat')); should work for ya Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208320 Share on other sites More sharing options...
Aegidius Posted April 29, 2011 Author Share Posted April 29, 2011 Thank you very much! I'll try and I'll let you know. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208321 Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 no problem..any trouble let me know Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208322 Share on other sites More sharing options...
trq Posted April 29, 2011 Share Posted April 29, 2011 @fugix, you are load to post links to scripts if you think they'll help a user out. eg; http://w-shadow.com/blog/2007/10/16/how-to-run-a-php-script-in-the-background/ Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208425 Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 okay, thank you Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208444 Share on other sites More sharing options...
Aegidius Posted April 30, 2011 Author Share Posted April 30, 2011 I have come to think that the best way to lunch a background script is with AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208590 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.