PHP + MySQL队列 [英] PHP + MySQL Queue
问题描述
我需要一个充当队列的简单表. 我的MySQL服务器限制是我不能使用InnoDB表,只能使用MyISAM.
I need a simple table that acts as a Queue. My MySQL server restriction is I can't use InnoDB tables, only MyISAM.
客户/工人将同时工作,并且每次都需要获得不同的工作.
Clients/workers will work at the same time and they will need to receive differents jobs each time.
我的想法是执行以下操作(伪代码):
My idea is to do the following (pseudo-code):
$job <- SELECT * FROM queue ORDER BY last_pop ASC LIMIT 1;
UPDATE queue SET last_pop WHERE id = $job->id
return $job
我曾经尝试过使用表锁和"GET_LOCK",但是什么也没发生,工人有时会得到相同的工作.
I had tried table lock and "GET_LOCK" but nothing happends, workers sometimes receives same jobs.
推荐答案
您需要调换顺序,以便没有计时窗口.
消费者POP(每个消费者都有一个唯一的$ consumer_id)
Consumer POP (each consumer has a unique $consumer_id)
Update queue
set last_pop = '$consumer_id'
where last_pop is null
order by id limit 1;
$job =
Select * from queue
where last_pop = '$consumer_id'
order by id desc
limit 1;
供应商推送
insert into queue
(id, last_pop, ...)
values
(NULL, NULL, ...);
队列由id列按时间排序,并根据POP分配给consumer_id.
The queue is ordered in time by the id column and assigned upon POP by to the consumer_id.
这篇关于PHP + MySQL队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!