Can serialized objects be accessed simultaneously by different processes, and how do they behave if so?

前端 未结 2 1220
别那么骄傲
别那么骄傲 2021-01-26 06:56

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 persis

2条回答
  •  青春惊慌失措
    2021-01-26 07:06

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

    1. They can both be served at once, however, if one user is reading while the other is writing the reading user may get strange results.
    2. Probably not. Once the tree has been read from the file into memory the other user will not see edits of the first user. If the tree hasn't been read from the file then the change will still be detected.
    3. Both changes will be made simultaneously and the file will likely be corrupted.

    Also, you mentioned shelve. From the shelve documentation:

    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.

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

提交回复
热议问题