If a user visits a site and his visit triggers a cron job that is quite intensive, the page load speed will be slower for him right? As I understand the page doesn't wait for the cron job to execute before loading, but since the cron job would be running in parallel, it might still be the case that the page loads slower since the server is busy right?
-
Have a read up on spawn_cron which might help you: codex.wordpress.org/Function_Reference/spawn_cron– TomCDec 10, 2013 at 18:31
-
If your cron job is heavy enough to make server busy then it is heavy enough to make server busy. I don't quite follow what is your question - to confirm that?– RarstDec 10, 2013 at 18:58
-
Yes I just wanted to confirm that, since I've been reading that page loads and cron jobs run independently and so the page load isn't affected. However if as you say the cron job is heavy enough it will affect page load as a side effect.– urok93Dec 11, 2013 at 16:30
2 Answers
Short answer - Nope. Any page request initializes the scheduled queue. It's just an initialize request. Wp-cron request is a standalone request.
so requesting URL
/somepage
you just initialize request to/wp-cron.php
However - If cron event doesn't work really well (it's has 1000 db queries e.g. or its requesting a some really long-to-respond resource), or both, or re-scheduling cron event for each request... just like any other http request it will eat resources, CPU performance, memory, etc... if it eats enough resources, your page will become slower.
The short answer is actually yes, in most cases.
Firstly, on most set-ups, spawning a cron job incurs a 1 second delay on page load, because it is done via a loopback HTTP request with a 1 second timeout - see https://wordpress.org/support/topic/save-a-full-second-on-cron-execution/.
Secondly, the spawned job will now be competing with the page load for database access (as well as other resources). Multiple processes can read the database concurrently; however, whenever a process is writing to the database, by default it is locked to prevent simultaneous write or read access by any other process - see https://stackoverflow.com/questions/1005206/does-sqlite-lock-the-database-file-on-reads#answer-1005218. The impact of this depends how complex the cron job's database updates are and how long the database is actually locked for, and may be insignificant. Of course, it would also be an issue if a cron job happened to be running when a page is requested, but having cron jobs spawned on page load guarantees they'll affect at least that page load.
If your server/hosting permits, you are recommended to set up a scheduled cron job to run every few hours, with the command
php -q /path/to/wp-cron.php
and disable cron spawning on page load with the following entry in wp-config.php
:
define('DISABLE_WP_CRON', true);
-
1
-
1According to the article, the 1 second delay happens on cURL versions below 7.15.5. It works fine on curl versions from 7.15.5 and above. 7.15.5 was released August 2006. There is no longer a 1 second timeout except on really really old setups. Aug 13, 2019 at 12:26
-
-
@user63350 That's true of cURL but not of WordPress. From the article: "the class WP_Http_Curl adjusts the fractional timeout to one full second (even if cURL would support it)". Even in the latest WordPress (5.2.2) the timeout is still 1 second, so the issue is present in all set-ups including the very latest.– JakeAug 29, 2019 at 17:42
-
2