WordPress 多个 ajax post 请求,get_option 不显示后续 ajax post 请求的更改
我有一个长期运行的发布请求处理程序,可以批量处理项目。我希望用户能够通过发送另一个发布请求随时取消此批次。我有一个存储在 wp_options 中的标志,但是,后面的取消请求似乎无法被第一个长期存在的发布请求处理程序读取。
示例:
add_action('wp_ajax_batch', 'my_ajax_batch_handler');
function my_ajax_batch_handler()
{
update_option('batch_state', 'running');
// process items one by one, if the batch_state is ever 'cancelled', then quit.
foreach ($values as $item) {
// $batch_state is never 'cancelled' here, always 'running' even when it has been
// set to 'cancelled' in the following function.
$batch_state = get_option('batch_state');
if (!batch_state or batch_state == 'cancelled') {
break;
}
}
}
add_action('wp_ajax_cancel_batch', 'my_ajax_cancel_batch_handler');
function my_ajax_cancel_batch_handler()
{
update_option('batch_state', 'cancelled');
// batch_state is cancelled here, but this change does not reflect in the
// loop in the previous function.
}
Wordpress/php 是否为每个 ajax post 请求提供隔离的环境?
I have a long-running post request handler that batch processes items. I want the user to be able to cancel this batch at any time by sending another post request. I have a flag that is stored in wp_options, however, the later cancel request does not appear to be able to be read by the first long-lived post request handler.
Example:
add_action('wp_ajax_batch', 'my_ajax_batch_handler');
function my_ajax_batch_handler()
{
update_option('batch_state', 'running');
// process items one by one, if the batch_state is ever 'cancelled', then quit.
foreach ($values as $item) {
// $batch_state is never 'cancelled' here, always 'running' even when it has been
// set to 'cancelled' in the following function.
$batch_state = get_option('batch_state');
if (!batch_state or batch_state == 'cancelled') {
break;
}
}
}
add_action('wp_ajax_cancel_batch', 'my_ajax_cancel_batch_handler');
function my_ajax_cancel_batch_handler()
{
update_option('batch_state', 'cancelled');
// batch_state is cancelled here, but this change does not reflect in the
// loop in the previous function.
}
Does Wordpress/php have isolated environments for each ajax post request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终通过在循环中调用 get_option 之前清除缓存来解决这个问题。
不确定为什么存在这种行为,但清除缓存可以解决它。
I ended up solving this issue by clearing the cache before the get_option call in the loop.
Not sure why this behavior exists, but clearing the cache solves it.