我应该如何在 Perl 中实现原子序列?
我有以下要求:
- 序列对于主机是唯一的(不需要共享增量)
- 序列必须单调递增。
- 该序列必须跨进程保持不变。
- 在多个进程同时处理序列的情况下,递增序列必须是原子的。
- 大多数情况下,文件会被更新,更新后会读取新值。但是,也应该可以在不更新的情况下读取当前值。
我可以将 Perl 代码拼凑起来,大致完成此操作,但我想要一个更优雅的解决方案。
I have the following requirements:
- The sequence is unique to a host (no shared incrementing is necessary)
- The sequence must be monotonically increasing.
- The sequence must be persistent across processes.
- Incrementing the sequence must be atomic in the case of multiple processes working on it at the same time.
- Most of the time the file will be updated and the new value read after update. But, it should also be possible to read the current value without update.
I can hack together perl code that will do roughly this, but I'd like a more elegant solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将序列号存储在文件中,并使用
flock
确保只有一个进程可以访问它:您可以将此调用为
get($file,0)
来检索序列号码而不改变它。Store the sequence number in a file and use
flock
to make sure only one process can access it:You can call this as
get($file,0)
to retrieve the sequence number without changing it.系统时间提供了一个单调递增的序列,它解决了(2):
直到有人重置时钟......
持久性(3)和增量的原子性(4)似乎需要锁定数据库。我想到了 Berkeley DB。但您可能正在寻找更简单的东西,除非您已经在使用它。阅读而不更新(5)是没有问题的。单调递增序列 (2) 也不会。
我不确定“主机唯一”和“共享增量”是什么意思(1)。如果来自不同主机的序列元素可以具有相同的值,那么您可以将该方法乘以所有服务器。否则,您只能拥有一个必须可供其他人通过网络访问的序列。
System time provides a monotonically increasing sequence, which addresses (2):
Until someone resets the clock ...
Persistence (3) and atomicity of incrementations (4) seem to require a locking database. Berkeley DB comes to mind. But you might be looking for something simpler, unless you're already using it anyway. Read without update (5) would be no problem. A monotonically increasing sequence (2) wouldn't be either.
I'm not sure what you mean by "unique to a host" and "shared increment" (1). If it okay for sequence elements from different hosts to have the same value, then you can multiply the approach to all servers. Else you can have only one sequence that must be accessible to others via the network.