我的回答很简单: 是否可以在PHP中使用system_v信号量来解决生产者 - 消费者问题? 我的意思是,可以找到经典的产品解决方案here。
semaphore fillCount = 0; // items produced
semaphore emptyCount = BUFFER_SIZE; // remaining space
procedure producer() {
while (true) {
item = produceItem();
down(emptyCount);
putItemIntoBuffer(item);
up(fillCount);
}
}
procedure consumer() {
while (true) {
down(fillCount);
item = removeItemFromBuffer();
up(emptyCount);
consumeItem(item);
}
}
正如您所看到的,生产者在将项目放入缓冲区后增加fillCount
。
无论如何,正如我在php-doc中所理解的,你只能使用sem_acquire和sem_release来操作信号量,你不能在没有获得它之前调用sem_release。
你知道如何解决这个问题吗?
答案 0 :(得分:1)
如果重要,请考虑使用真实的消息队列服务器:RabbitMQ,OpenAMQ,ActiveMQ,ZeroMQ,beanstalkd,等。使用信号量或其他本地机制不会扩展到单个服务器之外。