Bash队列 - 原子等待&锁

时间:2016-09-23 17:21:21

标签: multithreading bash queue atomic blockingqueue

我想在Bash中使用一个队列,一个进程(例如一个Bash脚本)将写入,其他几个进程将从中读取。

我想这样做:

作家:

# block if another process is reading or writing,
# then lock the queue using an exclusive lock
lock -ex queue  
echo "$message" >> queue
unlock queue

阅读器:

# wait until there is an item in the queue
# and then lock the queue using a shared lock
wait_while_empty_and_lock -sh queue
read -r message < <(tail -n+1 queue)
(do stuff...)
unlock queue

最好,我想使用Bash +标准/常用Linux / UNIX实用程序(不是Redis等)。我考虑了几种涉及flock,几种FIFO等的方法,但我无法得出令人满意的结果。有没有一种简单的方法可以在Bash / Linux / UNIX中执行此操作,还是必须使用Redis等第三方工具?

0 个答案:

没有答案