我应该如何在小型多线程 python 应用程序中共享和存储数据?
我正在编写一个小型多线程客户端 python 应用程序,其中包含一个小型网络服务器(仅向本地主机提供页面)和一个守护进程。网络服务器加载数据并将其放入持久的“数据存储”中,守护进程处理这些数据,修改它并添加更多数据。它还应该负责与磁盘的同步。
我想尽可能避免复杂的外部事物,例如 SQL 或其他数据库。
设计数据存储区有哪些好的、简单的方法?如果您的解决方案仅使用标准 python,则可以获得加分。
I'm writing a small multithreaded client-side python application that contains a small webserver (only serves page to the localhost) and a daemon. The webserver loads and puts data into a persistent "datastore", and the daemon processes this data, modifies it and adds some more. It should also takes care of the synchronization with the disk.
I'd like to avoid complicated external things like SQL or other databases as much as possible.
What are good and simple ways to design the datastore? Bonus points if your solution uses only standard python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你所寻求的并不是太特定于Python,因为AFAIU你想要在两个不同的进程之间进行通信,而这两个进程只是偶然用Python编写的。如果这确实是您的问题,您应该寻找一种通用的解决方案,而不是特定于 Python 的解决方案。
我认为简单的 No-SQL 键值数据存储(例如 Redis)可能是适合您的情况的一个非常好的解决方案。与“复杂”相反,使用专门为此目的设计的工具实际上会使您的代码更简单。
如果您坚持仅使用 Python 解决方案,请考虑使用随 Python 预装的 SQLite 的 Python 绑定。只要数据访问的语义定义良好(即无论如何都必须解决的问题,不管工具如何),SQLite DB 就可以安全地由两个进程同时使用。
What you're seeking isn't too Python specific, because AFAIU you want to communicate between two different processes, which are only incidentally written in Python. If this indeed is your problem, you should look for a general solution, not a Python-specific one.
I think that a simple No-SQL key-value datastore such as Redis, for example, could be a very nice solution for your situation. Contrary to "complicated" using a tool designed specifically for such a purpose will actually make your code simpler.
If you insist on a Python-only solution, then consider using the Python bindings for SQLite which come pre-installed with Python. An SQLite DB can be concurrently used by two processes in a safe manner, as long as your semantics of data access are well defined (i.e. problems you have to solve anyway, the tool nonwithstanding).