views:

496

answers:

7

Hi,

I have a two websites in php and python. When a user sends a request to the server I need php/python to send an HTTP POST request to a remote server. I want to reply to the user immediately without waiting for a response from the remote server.

Is it possible to continue running a php/python script after sending a response to the user. In that case I'll first reply to the user and only then send the HTTP POST request to the remote server.

Is it possible to create a non-blocking HTTP client in php/python without handling the response at all?

A solution that will have the same logic in php and python is preferable for me.

Thanks

A: 

have you already thought about an ajax-based solution?

Jochen Hilgers
This should be a comment, not an answer.
idrumgood
It's server side problem and not a browser problem.Ajax is not relevant.
pablo
A: 

What you need to do is have the PHP script execute another script that does the server call and then sends the user the request.

Anthony
php doesn't have threads
pablo
Right, you would do it either over exec() or cURL. You aren't starting a thread, you are just initiating another script that's running independently of your one that deals with the user response.
Anthony
So if exec() is too slow, just do it via cURL. Make a post request to the other script with whatever data from the user request the secondary script needs to send to the remote server. Set the secondary script to respond to your main script with "Got it, thanks" and have it keep going after that.
Anthony
+1  A: 

You can spawn another process to handle the POST to the other server. In PHP you would spawn the process and "disconnect" so you don't wait for the response.

exec("nohup /path/to/script/post_content.php > /dev/null 2>&1 &");

You can then you curl to perform the post. If you want to pass parameters to the PHP script, you can use the getopt() function to read them. Not sure if you would do something similar in Python.

Brent Baisley
using exec requires a lot of resource and will be slow.
pablo
+3  A: 

You can do this in PHP using cURL. Here's an example of making requests using curl in a non-blocking (or asynchronous) manner.

cURL is available for Python as well.

Travis Beale
+2  A: 

In PHP you can close the connection by sending this request (this is HTTP related and works also in python, although I don't know the proper syntax to use):

// Send the response to the client
header('Connection: Close');
// Do the background job: just don't output anything!

Addenum: I forgot to mention you probably have to set the "Context-Length". Also, check out this comment for tips and a real test case.

ntd
how will it work in a framework? For example in MVC framework?Do I have to do it as part of the framework itself as the last step or can I just use it in one of the controllers?
pablo
I don't know your use case. If you need to perform a _new_ operation in background, I'd enclose the whole framework in the `ob_start()`...`ob_end_flush()` pair. But here I'm guessing. MVC is an empty word for me... the better candidate would be the controller though.
ntd
...the **best** candidate...
ntd
A: 

Hookah is designed to solve your problem.

Dustin
A: 

in python you could simply close the output stream, then continue to do your post request

sys.stdout.close()
cobbal
This is interesting.Will it work with django?Django have middleware run after the view returns so I'm not sure it will be non-blocking for the client or might cause undesired behaciour.
pablo