序列化的对象可以由不同的进程同时访问吗?如果是的话,它们的行为如何? [英] Can serialized objects be accessed simultaneously by different processes, and how do they behave if so?

查看:71
本文介绍了序列化的对象可以由不同的进程同时访问吗?如果是的话,它们的行为如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有最好用树表示的数据.序列化结构是最有意义的,因为我不想每次都对它进行排序,这将使我能够对数据进行持久性修改.

I have data that is best represented by a tree. Serializing the structure makes the most sense, because I don't want to sort it every time, and it would allow me to make persistent modifications to the data.

另一方面,将从不同计算机上的不同进程访问此树,因此我担心读写的细节.基本搜索在该主题上的收益不高.

On the other hand, this tree is going to be accessed from different processes on different machines, so I'm worried about the details of reading and writing. Basic searches didn't yield very much on the topic.

  1. 如果两个用户同时尝试恢复该树并从中读取内容,是否可以同时为其提供服务,还是首先随机发生一个?
  2. 如果两个用户打开了树(假设他们可以)并且一个进行了编辑,另一个用户是否看到已实施的更改? (我想他们没有,因为他们每个人都收到了相当于原始数据的副本.)
  3. 如果两个用户同时更改对象并关闭它,是不是先出现一个对象,还是试图同时进行两个更改?

我当时正在考虑将要应用到该树的更改排队,然后让该树按提交顺序执行它们.我以为我会问自己的问题,然后再尝试解决任何一个问题.

I was thinking of making a queue of changes to be applied to the tree, and then having the tree execute them in the order of submission. I thought I would ask what my problems are before trying to solve any of them.

推荐答案

如果没有尝试,我很确定答案是:

Without trying it out I'm fairly sure the answer is:

  1. 它们都可以一次送达,但是,如果一个用户正在阅读而另一用户正在书写,则该阅读用户可能会得到奇怪的结果.
  2. 可能不是.一旦将树从文件中读取到内存中,其他用户将看不到第一个用户的编辑.如果尚未从文件中读取树,则仍将检测到更改.
  3. 这两项更改将同时进行,并且文件可能已损坏.

您还提到了搁置.从货架文档中:

Also, you mentioned shelve. From the shelve documentation:

搁架模块不支持对以下文件的并发读/写访问 搁置的对象. (多个同时读取访问是安全的.) 一个程序有一个可供写的架子,任何其他程序都不应有 它可供阅读或写作. Unix文件锁定可以用来解决 这个,但是这在Unix版本之间是不同的,并且需要知识 关于所使用的数据库实现.

The shelve module does not support concurrent read/write access to shelved objects. (Multiple simultaneous read accesses are safe.) When a program has a shelf open for writing, no other program should have it open for reading or writing. Unix file locking can be used to solve this, but this differs across Unix versions and requires knowledge about the database implementation used.

就个人而言,您可能现在想研究使用像Redis这样的简单键值存储以及某种乐观锁定的方法.

Personally, at this point, you may want to look into using a simple key-value store like Redis with some kind of optimistic locking.

这篇关于序列化的对象可以由不同的进程同时访问吗?如果是的话,它们的行为如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆