4

I wasn't sure how to ask this. In WP, when the program runs, is it all "sequential"? By this I mean does WP hit the hook you registered and call it, wait, then proceed, synchronous? There's no Dependency Injection of IoC that I could find and no async.

I looked a the core, and I couldn't tell. I saw references to a few global variables and an array of hooks iterated over, but I did not understand the execution. I tried to with xdebug, but I have not grasped it yet.

5

The do_action() and apply_filters() are both wrappers of the

WP_Hook::apply_filters()

method, that invokes the registered callbacks in sequential order (src).

Here's a simple test:

Let's define a callback wrapper:

$callback_gen = function( $seconds ) { 
    return function() use ( $seconds ) {
        sleep( $seconds ); 
        printf( '%s finished' . PHP_EOL, $seconds ); 
    };
};

Next we register the callbacks to the mytest action:

add_action( 'mytest', $callback_gen( 3 ) );
add_action( 'mytest', $callback_gen( 5 ) );
add_action( 'mytest', $callback_gen( 1 ) );

Then we invoke the callbacks and measure the time it takes:

$start = microtime( true );
do_action( 'mytest' );
$stop = microtime( true );

and display it with:

echo number_format( $stop - $start, 5 );

Here are outputs from 5 test runs:

3 finished 
5 finished 
1 finished 
9.00087

3 finished 
5 finished 
1 finished 
9.00076

3 finished 
5 finished 
1 finished 
9.00101

3 finished 
5 finished 
1 finished 
9.00072

3 finished 
5 finished 
1 finished 
9.00080

where the order is the same for each run, with total of c.a. 9 seconds, as expected for a sequential run order.

4

Yes it's all linear/sequential. There is no multi-threading or parallel execution in a PHP application. WordPress hooks run sequentially.

When you hook into an action or filter, your code is just adding a callable object to an array somewhere. When the action fires, WordPress loops over those callables and calls them. As a sidenote, actions and filters use the same system and are technically the same.

It makes sense to think or actions and hooks as manually fired events in a linear timeline, rather than callbacks from a process in another thread,

Based on how things work in say Node, you may believe you can use hooks to hive off long running tasks or costly work to another thread. This is not the case, and running an infinite loop in a hook will cause the process to timeout. PHP processes run singlethreaded, and are not threadsafe, you can spawn additional processes, but that's entirely for you to manage, and has it's own issues.

  • 1
    So you could, in theory, write something that block and hang execution (say you created an infinite loop or waiting on some calculation)? – johnny May 5 '17 at 19:10
  • 1
    Yes, you could do that – swissspidy May 5 '17 at 21:15
  • @johnny, in real live, no. PHP default settings when used in web server enviroment will terminate the "process" after 30 seconds. You can extend it but if you are working in a web server enviroment it will probably mean that the connection will have to stay open otherwise the web server might terminate the process. – Mark Kaplun May 6 '17 at 5:47
  • hmmm some research later, and it seems like you can keep php alive, but ,most likely it will still cost you one worker in apache reducing the performance of your site – Mark Kaplun May 6 '17 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.