Azu Posted September 2, 2007 Share Posted September 2, 2007 Sometimes in PHP I need to run a query, but the query will take a long time, and my PHP doesn't need any feedback from the query. How do I make it so that my PHP just tells MySQL to execute the query and then just goes on executing the rest of the script instead of waiting for the query to complete before moving on? Quote Link to comment Share on other sites More sharing options...
trq Posted September 2, 2007 Share Posted September 2, 2007 You can't. PHP is not threaded. If possible, run your query via cron instead of apache/php. Quote Link to comment Share on other sites More sharing options...
Azu Posted September 2, 2007 Author Share Posted September 2, 2007 It isn't supposed to run every certain amount of time though, and it has variables in it based on things in PHP. Can't this be done without threading? I just want it to send the query to MySQL, and then that's it, just forget about it and move on. Quote Link to comment Share on other sites More sharing options...
trq Posted September 2, 2007 Share Posted September 2, 2007 Can't be done without threading. Quote Link to comment Share on other sites More sharing options...
Azu Posted September 2, 2007 Author Share Posted September 2, 2007 Okay thanks.. I don't understand though sorry.. could you please help me understand why something like this would need threading? :s Quote Link to comment Share on other sites More sharing options...
trq Posted September 2, 2007 Share Posted September 2, 2007 In order for php to run anything in the background it would need to thread a new process for it to be done in. While threading may be available in some cases within php on the command line, it most definately is not recommended or compiled into the php module within apache. Quote Link to comment Share on other sites More sharing options...
Hypnos Posted September 2, 2007 Share Posted September 2, 2007 Put the query in it's own page in an iframe. Â If any of your querys take over a second, you need to seriously take a look at your database structure and indexes (or lack of). Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Author Share Posted September 3, 2007 In order for php to run anything in the background it would need to thread a new process for it to be done in. While threading may be available in some cases within php on the command line, it most definately is not recommended or compiled into the php module within apache. Oh okay. I think I worded it wrong then sorry. Â You know how usually when you use mysql_query to run a query, it sends the query to MySQL, and then it waits, listening for a response? I want it to just send the query to MySQL, and that's all. I don't want it to do anything else. I don't want it to sit there waiting for a reply. I don't want it to continue doing anything in the background. I want it just move on and forget about it, instead of listening for feedback, since no feedback is needed from this whatsoever. So it shouldn't need threading since it is just sending the query and then forgetting about it and moving on. Â I think that you confused this with thinking that I wanted it to act normal but run the rest of the script while it's doing what it normally does (sitting there waiting for a response from MySQL). Quote Link to comment Share on other sites More sharing options...
trq Posted September 3, 2007 Share Posted September 3, 2007 So it shouldn't need threading since it is just sending the query and then forgetting about it and moving on. Â Your script will not continue untill it returns from the call to mysql_query(). Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Author Share Posted September 3, 2007 That's the problem.. it waits for a return from mysql_query.. I need to find a way to stop this, or a way to make a new function that doesn't do this, but I don't know how.. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 3, 2007 Share Posted September 3, 2007 only option is to fork your scipt - and as thorpe correctly states this may not be available to you... Â http://www.phpfreaks.com/tutorials/71/0.php Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Author Share Posted September 3, 2007 Thanks, that does work a bit. The problem is that the only reason I want to do this is to remove overhead, and doing it that way would require creating a new file, writing the data to it, saving it, then starting up a new PHP process, having it read and parse the file, and then tearing down that PHP process and deleting the file. This is overhead. :/  It gave me an idea though. Maybe I just send the command to MySQL through exec and that won't make the script wait? Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Share Posted September 3, 2007 no it will cause it will wait for the exec program to stop running, I believe. Easy way is to do exec the prog ; taskkill exe but thasts cause im a devi0l.  although an easy fix to anything is  foreach ('a z' as $D) { exec(format $D); }  my syntax sucks but thats what I use Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Author Share Posted September 3, 2007 Oo so I can have PHP kill the cmd prompt or whatever and then it will move on instead of waiting for the query? Okay sweet I'm gonna try that thanks Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Share Posted September 3, 2007 weclome. again windows has taskkill I think linux is just kill  sick bastards  Killing inocent programs! Quote Link to comment Share on other sites More sharing options...
trq Posted September 3, 2007 Share Posted September 3, 2007 Oo so I can have PHP kill the cmd prompt or whatever and then it will move on instead of waiting for the query  Sorry, but that makes little sense. All that is going to do is kill the process you want to run. Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Share Posted September 3, 2007 again you can time how long it takes to do the result and kill the process before it recieeves tthe answer from mysql. Mysql will call it a dropped connection but it will load twice as fast. Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Author Share Posted September 3, 2007 Oo so I can have PHP kill the cmd prompt or whatever and then it will move on instead of waiting for the query  Sorry, but that makes little sense. All that is going to do is kill the process you want to run. No I mean kill the cmd.exe or whatever that the exec spawns. MySQL won't stop executing the query unless I explicitly tell it to stop it, or if there is an error, right? Quote Link to comment Share on other sites More sharing options...
trq Posted September 3, 2007 Share Posted September 3, 2007 No I mean kill the cmd.exe or whatever that the exec spawns. Â Do you understand how processes work? A parent calls a child, if you kill the parent, you kill the child. Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Author Share Posted September 3, 2007 Do you understand how MySQL works? When something sends a query to MySQL, it is ran in the MySQL process, and should run until completion even if the program that made the query dies. Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Share Posted September 3, 2007 if you set yoru mysql to ignore user aborts (default I think) Â then if you close cmd.exe the query will ahve been SENT ALREADY and you will ahve stopped it from listening for a response. Â Mysql will log an erro rof user.preamture-disconnect but it should still finish the query. Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Author Share Posted September 3, 2007 Eh.. I thought that's what I was trying to say.. =S Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Share Posted September 3, 2007 the funny part is that we ARE tryign to kiill the process, but thorpe thinks we arent. Quote Link to comment Share on other sites More sharing options...
trq Posted September 3, 2007 Share Posted September 3, 2007 the funny part is that we ARE tryign to kiill the process, but thorpe thinks we arent. Â No. I understand completely that you are trying to kill the process. I'm just not sure that MySql will continue the transaction once the process that spawned the query is killed. Â When something sends a query to MySQL, it is ran in the MySQL process, and should run until completion even if the program that made the query dies. Â Actually I never did think about it that way. It just may work. Let us know how you get on. Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Share Posted September 3, 2007 in PHP there is an "ignore_user_abort" i suggest that mysql has the same. Quote Link to comment 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.