Jump to content

Recommended Posts

Hi there,

I was wondering if anyone has any tips for Multithreading in PHP 7.

I've done some preliminary research and not sure which of the following I should be using?

1) Use built in Thread Object in the PHP Language

2) cURL ?

https://www.reddit.com/r/PHP/comments/3a2yyu/async_in_php/

3) This exec hack

https://medium.com/async-php/multi-process-php-94a4e5a4be05#.uiboo7tro

4) Some 3rd party Library I'm not aware of? Icicle?

 

I'm implementing a new search that searches the title, contents of the article and the author names. It would normally be 3 sequential searches followed by formatting the results before outputting.

Would like to implement some asynchronous searches here and keep it if it runs faster than the sequential counterpart.

Thanks in advance!

 

 

 

Link to comment
Share on other sites

43 minutes ago, LostKobrakai said:

Afaik the most mature tool for async php is reactphp. But really async code is not a peace of cake and there are probably better languages out there than php to execute code concurrently.

 
 

I was afraid someone might say this. I haven't seen anything out there that indicates otherwise. 
 

Link to comment
Share on other sites

I've done a thing like this:

Here (not in this part of the script) I test for all variables, when oké, I'll post to the same URL with an added URLSegment called 'curl'. So the the script will be executed again, but then over shell_exec an out of the session.

You should note that all output even errors will have a nice journey into /dev/null so they are gone. For error handling you're on your own.

// Success
if (!count($this->errors)) {
	$query = http_build_query($this->postArray);
	// point output to /dev/null/
	shell_exec("curl --data '$query' " . $this->page->httpUrl . "curl/ 2>/dev/null >/dev/null &");
	$this->json['success'] = true;

// Show errors, abort
} else {
	$this->json['errors'] = $this->errors;
}

 

  • Like 2
Link to comment
Share on other sites

@Martijn Geerts So that's a kinda fire and forget approach? There's no way to retrieve any of the output of that curl request.

@FrancisChung I had great joy exploring a bit about what's possible with elixir. It's a great language for concurrent programming and not as hard to grasp as erlang even though it runs on the same runtime.

  • Like 1
Link to comment
Share on other sites

@FrancisChung

Is this for a search you want to kick off via a page on your PW site - where the user can enter some variable search term? If so, how about a Click() handler on the submit button that issues 3 distinct ajax searches against the backend?

Here's some pseudo-JQuery to illustrate the idea. To keep it shorter, this example kicks off two async ajax calls to the back-end to get search results as JSON and then renders the output once both calls have returned (either with or without data.)

$('#submit-button').click(function () {
    var searchA = false, searchB = false;

    // Kick off asynchronous call to get results for search A...
    // return results as JSON
    jQuery.ajax({
        url: '', // Construct a url to trigger search A
        success: function (result) {
            searchA = true;
            // Add code to store result JSON...

            rendezvous(searchA, searchB);
        }
    });

    // Kick off asynchronous call to get results for search B...
    // return results as JSON
    jQuery.ajax({
        url: '', // Construct a url to trigger search B
        success: function (result) {
            searchB = true;
            // Add code to store result JSON...

            rendezvous(searchA, searchB);
        }
    });
});

function rendezvous(A, B) {
    if (A & B) {
        // Use JQuery to combine, format & render search results in your current page
    }
}

 

  • Like 3
Link to comment
Share on other sites

I've used method 3) before, though I don't consider it a hack at all. That being said, I chose this route mainly because I strongly dislike a) complexity and b) dependencies, and that right there is a straightforward way to get some asynchronous PHP magic up and running in no time. I also didn't have any need to track the return values etc. of started processes, so no real need to keep track of them either; that alone adds quite a bit of complexity, and may be a valid reason to use something like React instead.

Anyway, I think @netcarver's solution makes most sense here. I'd also opt for a JavaScript based solution. If the generated data is going to require some heavy lifting behind the scenes, you should consider (pre-)caching it, but other than that a client-side implementation often makes most sense.

  • Like 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...