detaching the response

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Woody222
Forum Newbie
Posts: 2
Joined: Sat Nov 14, 2009 12:23 am

detaching the response

Post by Woody222 »

is there any way in php to send back the response and finish the request response cycle but after that run more php code. The idea is I have the user input something and it is validated and then some heavy processing is done before a value is put into the database. What I want to do is after the validation send the response back to the user (exit or die normally) and then the processing be done on the server while the user isn't worrying about it at all. I hope I've got across the idea. I thought I could put the processy bit in a different php call and at the moment once the first one is done through ajax a second ajax request is sent for the processy bit, however I'm worried about relying on client side and would feel much better if once the first request was made I could be sure the whole operation would execute.

Woody

p.s. I dont know if this should be in code or design and theory, if it should be in code can a mod switch it please.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: detaching the response

Post by alex.barylski »

User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: detaching the response

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: detaching the response

Post by alex.barylski »

Abort Flush Captain :P
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: detaching the response

Post by josh »

Even with the flushing functions sometimes the response still gets "chunked" by the web server or browser.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: detaching the response

Post by Christopher »

How about using something like Gearman?
(#10850)
Woody222
Forum Newbie
Posts: 2
Joined: Sat Nov 14, 2009 12:23 am

Re: detaching the response

Post by Woody222 »

ok I think I've figured a solution. Split the processy bit and the validation in to 2 php files. The first is validation files calls the second and then cuts the connection after 1 flush. The second file uses the abort setting then flushs ok back to the first page. To the user they see the first page finish and be told the validation is ok while the process can process unknown to the user.

any one is any problems with this?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: detaching the response

Post by josh »

You could do

Code: Select all

 
exec( '/path/to/php myTask.php &' );
 
which would spawn a background task

You could also use the 'nice' command to set the processing priority. This would restrict you in terms of compatible hosting.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: detaching the response

Post by Jenk »

Post Reply