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).

hakre
  • 193,403
  • 52
  • 435
  • 836
Basic
  • 26,321
  • 24
  • 115
  • 201
  • @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. – Basic Jul 03 '12 at 09: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 – hakre Jul 03 '12 at 09:07
  • @hakre Thanks, that's a useful tip (and not a problem). It's actually 5.4 so I'll remove the 5.3. – Basic Jul 03 '12 at 09:48
  • Take a look at this [post][1] It worked for me [1]: http://stackoverflow.com/a/13690590/599993 – jzafrilla Dec 04 '12 at 09:02

2 Answers2

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
?>
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
Alexei Tenitski
  • 9,030
  • 6
  • 41
  • 50
  • 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 – Basic May 07 '11 at 10:20
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
Rango
  • 11
  • 1