You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP单线程环境下wp-cron后台运行及非阻塞实现问询

How does wp-cron run in the background when PHP is single-threaded?

Great question—this is a common point of confusion because WordPress's cron system works very differently from traditional server cron jobs, especially when paired with PHP's single-threaded per-request model. Let's break this down step by step:

First, a key clarification: wp-cron isn't a "background daemon"

Unlike system cron (which runs as a persistent background process), wp-cron is request-triggered. It only springs into action when a web request hits your WordPress site—whether that's from a user visiting a page, a search bot crawling, or a scheduled system call.

How it avoids blocking client responses

PHP's single-threaded nature applies to individual requests—but WordPress uses a clever trick to offload cron tasks to separate, non-blocking processes:

  • Asynchronous HTTP requests: When WordPress detects pending cron tasks during a page load, it calls the spawn_cron() function. This function initiates an asynchronous HTTP request to your site's wp-cron.php endpoint (using tools like curl or fsockopen).

    • The critical detail here is that this request is set to timeout immediately (or after a tiny window), so the original page request can finish sending a response to the client without waiting for the cron tasks to complete.
    • The asynchronous request spins up a brand new PHP process (separate from the one handling the user's page load) to run the cron tasks. Since PHP processes are isolated, this new process runs independently—no single-thread conflict here.
  • Fallback for servers without async support: If your server blocks curl/fsockopen, WordPress falls back to running cron tasks at the end of the current request (after sending the response headers to the client). While this is technically part of the same PHP thread, it happens after the client has already received their page content, so it doesn't feel like a block. For longer tasks, WordPress also supports splitting work into smaller batches via hooks to avoid noticeable delays.

Bonus: Ditching user-triggered cron entirely

If you want to avoid relying on user traffic to run cron tasks (which can be unreliable for low-traffic sites), you can set up a system-level cron job to call wp-cron.php on a schedule. For example, adding this to your server's crontab:

* * * * * wget -q -O - https://your-site.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1

This runs a separate, independent request every minute to trigger cron tasks—completely decoupled from user page loads, with zero blocking involved.

To wrap up

PHP's single-threaded per-request model doesn't hinder wp-cron because:

  • Cron tasks run in separate PHP processes via asynchronous requests.
  • Even when falling back to in-request execution, tasks run after the client response is sent.
  • System cron can replace user-triggered runs for full reliability.

内容的提问来源于stack exchange,提问作者learning_13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:43:40