QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads How WordPress executes Async Cron without effecting page load time

How WordPress executes Async Cron without effecting page load time

WordPress Cron Jobs are a great way to schedule events which occur at specific interval of time.

WordPress cron jobs run when current time is equal or more than the scheduled time of the cron job. WordPress makes this check whenever a HTTP request is sent to WordPress. If the scheduled time has occurred then WordPress runs the event during that HTTP request. But the best thing is that execution of cron jobs don’t effect the page load time. Although they are started during HTTP requests but they actually runs asynchronous to the HTTP request.

How does WordPress run Cron Jobs Asynchronously

There are basically two ways in PHP to run code asynchronous to the HTTP request execution

  1. You can use PHP program execution functions to run a new process parallel to the current HTTP request executing process. The new process will execute the scheduled event. But the problem with this method is that many shared hosting servers don’t support these PHP functions.
  2. You can send a HTTP request to a different PHP page in the same server which is responsible for running the scheduled event. This solution is better because every hosting server supports CURL library and file_get_contents function.

WordPress uses the second method. When WordPress needs to run a cron event it simply makes a HTTP request to wp-cron.php file passing the cron job name. WordPress doesn’t wait for the HTTP request to finish i.e., the HTTP request is made asynchronous to the current page execution. Therefore there is no way it would effect page load time.

Example

WordPress uses WordPress HTTP API to make a request to the wp-cron.php file.

//timeout indicates the maximum number of seconds we allow the cron job to execute.
//blocking indicates weather we want to allow PHP to continue execution while the transport is working. Setting it to false makes the request happen in background and therefore it doesn't effect current page load time. Internally true value uses synchronous sockets and false value uses asynchronous sockets.

$args = array(
    "method" => "GET",
    "timeout" => 100,
    "blocking" => false
);
wp_remote_request("http://domain.name/wp-cron.php?job=backup", $args);
Dec 28, 2014Narayan Prusty
Twitter Auto Favorite WordPress PluginPHP Asynchronous Cron Job - Scheduling Tasks

Leave a Reply Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

7 years ago WordPress
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
  • How does WordPress run Cron Jobs Asynchronously
  • Example
Related Articles
  • Scheduling Events In WordPress Using Cron Jobs
  • Increase PHP Script Execution Time
  • PHP Asynchronous Cron Job – Scheduling Tasks
  • WordPress Plugin Activation, Deactivation and Uninstall Hooks
  • Creating A Viewport Resizer
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license