Is the WordPress Cron API running in the background?

I am reading the cron.php code in wp-include and spawn_cron() , it seems the one that actually performs the registered tasks.

The last two lines of the function:

 $cron_url = site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron ); wp_remote_post( $cron_url, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) ); 

It simply opens wp-cron.php, passing the task as a request argument.

API description at the top of cron.php:

 * Schedules a hook which will be executed once by the WordPress actions core at * a time which you specify. The action will fire off when someone visits your * WordPress site, if the schedule time has passed.` 

My question is that they say that the visitor opens one of the pages of the site, and then the registered task is launched by the cron API. And if the task is difficult and takes several minutes, does the visitor get a page that is not fully loaded until the task is completed?

[Edit] To clarify what I'm asking, the question is, does the WP Cron API launch tasks after the page has finished loading?

+4
source share
2 answers
 $cron_url = site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron ); wp_remote_post( $cron_url, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) ); 

With the plugin connected below, I confirmed that the code above (I also quoted in the question) actually calls the scheduled tasks. It sets a 0.0.1 timeout and accesses wp-cron.php. This means that for 100 tasks it takes 1 second to load all tasks. Thus, this slightly affects the page loading speed. But there seems to be something not to worry too much about yourself.

 <?php /* Plugin Name: Sample Cron Task */ // I used `heavy` because this code was initially written to test how it affects the server response if a heavy task is registered as a cron job. So forget about the naming. add_action('admin_menu', 'sample_cron_heavy_task'); function sample_cron_heavy_task() { add_options_page( 'Sample Cron Heavy Task', 'Sample Cron Heavy Task', 'manage_options', 'sample_cron_heavy_task', 'sample_cron_heavy_task_admin'); } function sample_cron_heavy_task_admin() { ?> <div class="wrap"> <?php wp_schedule_single_event(time(), 'my_action_name'); $cron_url = site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron ); // executes the registered task immediately wp_remote_post( $cron_url, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) ); echo get_option('sample_cron_heavy_task'); ?> </div> <?php } add_action('my_action_name', 'myevent'); function myevent() { $msg = date('Y mdh:i:s A') . ': the cron task is called.<br />'; update_option('sample_cron_heavy_task', $msg); } 
+2
source

The cron task should not affect your viewer. If he finds a new hosting company.

-1
source

Source: https://habr.com/ru/post/1433923/


All Articles