How do I notify a process of an SQLite database change done in a different process?

前端 未结 7 2540
南笙
南笙 2021-02-19 16:27

Let\'s say I have two or more processes dealing with an SQLite database - a \"player\" process and many \"editor\" processes.

The \"player\" process reads the database

7条回答
  •  失恋的感觉
    2021-02-19 17:18

    A relational database is not your best first choice for this.

    Why?

    You want all of your editors to pass changes to your player.

    Your player is -- effectively -- a server for all those editors. Your player needs multiple open connections. It must listen to all those connections for changes. It must display those changes.

    If the changes are really large, you can move to a hybrid solution where the editors persist the changes and notify the player.

    Either way, the editors must notify they player that they have a change. It's much, much simpler than the player trying to discover changes in a database.


    A better design is a server which accepts messages from the editors, persists them, and notifies the player. This server is neither editor nor player, but merely a broker that assures that all the messages are handled. It accepts connections from editors and players. It manages the database.

    There are two implementations. Server IS the player. Server is separate from the player. The design of server doesn't change -- only the protocol. When server is the player, then server calls the player objects directly. When server is separate from the player, then the server writes to the player's socket.

    When the player is part of the server, player objects are invoked directly when a message is received from an editor. When the player is separate, a small reader collects the messages from a socket and calls the player objects.

    The player connects to the server and then waits for a stream of information. This can either be input from the editors or references to data that the server persisted in the database.

    If your message traffic is small enough so that network latency is not a problem, editor sends all the data to the server/player. If message traffic is too large, then the editor writes to a database and sends a message with just a database FK to the server/player.


    Please clarify "If the editor crashes while notifying, the player is permanently messed up" in your question.

    This sounds like a poor design for the player service. It can't be "permanently messed up" unless it's not getting state from the various editors. If it's getting state from the editors (but attempting to mirror that state, for example) then you should consider a design where the player simply gets state from the editor and cannot get "permanently messed up".

提交回复
热议问题