4

Scenario is as follows:

Call to a specified URL including the Id of a known SearchDefinition should create a new Search record in a db and return the new Search.Id.

Before returning the Id, I need to spawn a new process / start async execution of a PHP file which takes in the new Search.Id and does the searching.

The UI then polls a 3rd PHP script to get status of the search (2nd script keeps updating search record in the Db).

This gives me a problem around spawning the 2nd PHP script in an async manner.

I'm going to be running this on a 3rd party server so have little control over permissions. As such, I'd prefer to avoid a cron job/similar polling for new Search records (and I don't really like polling if I can avoid it). I'm not a great fan of having to use a web server for work which is not web-related but to avoid permissions issues it may be required.

This seems to leave me 2 options:

  • Calling the 1st script returns the Id and closes the connection but continues executing and actually does the search (ie stick script 2 at the end of script 1 but close response at the append point)
  • Launch a second PHP script in an asynchronous manner.

I'm not sure how either of the above could be accomplished. The first still feels nasty.

If it's necessary to use CURL or similar to fake a web call, I'll do it but I was hoping for some kind of convenient multi-threading approach where I simply spawn a new thread and point it at the appropriate function and permissions would be inherited from the caller (ie web server user).

4
  • @hakre I'm going to keep rolling back until someone gives me a good reason otherwise - The software in use is php5 and php5 has features which are relevant to the question, specifically the ability to fork. Commented Jul 3, 2012 at 9:00
  • Which PHP version? If it really matters (I had no intent to cut away information from your question), please name the PHP version: PHP 5.x.y Commented Jul 3, 2012 at 9:07
  • @hakre Thanks, that's a useful tip (and not a problem). It's actually 5.4 so I'll remove the 5.3. Commented Jul 3, 2012 at 9:48
  • Take a look at this [post][1] It worked for me [1]: stackoverflow.com/a/13690590/599993 Commented Dec 4, 2012 at 9:02

2 Answers 2

7

I'd rather use option 1. This would also keep related functionality closer to each other. Here is a hint how to send something to user and then close the connection and continue executing:

(by tom ********* at gmail dot com, source: http://www.php.net/manual/en/features.connection-handling.php#93441)

<?php
ob_end_clean();
header("Connection: close\r\n");
header("Content-Encoding: none\r\n");
ignore_user_abort(true); // optional
ob_start();
echo ('Text user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();     // Strange behaviour, will not work
flush();            // Unless both are called !
ob_end_clean();

//do processing here
sleep(5);

echo('Text user will never see');
//do some processing
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm, thanks - I actually ended up wrapping it in a function as demonstrated in the post below that but the core functionality is the same
0

swoole: asynchronous & concurrent extension. https://github.com/matyhtf/swoole

  • event-driven
  • full asynchronous non-blocking
  • multi-thread reactor
  • multi-process worker
  • millisecond timer
  • async MySQL
  • async task
  • async read/write file system
  • async dns lookup

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.