通过MacOS上的几个无关进程对一个文件的同时访问一个文件

发布于 2025-02-06 05:44:46 字数 1299 浏览 2 评论 0原文

我需要在MacOS系统上获得几个过程,以相互通信。这些过程每天将在不同时间产生几次,我无法预测它们何时会同时启动(如果有的话)。这些程序在 python swift

如何安全地允许这些程序全部写入同一文件?

我探索了一些不同的选择:

  • 我想使用 sqlite 3 但是,我在文档中找不到有关是否安全地在过程中同时写入的答案。此问题 不是很确定,旧的,我理想地希望获得更具权威性的答案。 /p>

  • 我想到了使用多次浏览在支撑锁时。但是,据我在文档中看到的,您需要一个元过程来产生孩子并在最长的儿童过程中保留。我很好地拥有一个元源过程,但是要有一个元过程基本上熬夜,只是为了解决矛盾的访问吗?

  • 沿着此目的,我想到的是整天呆在一起的过程,并从所有其他过程中接收消息,并且是唯一负责写入文件的过程。感觉有些浪费,我应该多么担心整天进行程序的资源成本,而做得很少?是唯一担心内存足迹和CPU使用情况(如活动监视器所示),还是还有其他重大成本,例如上下文切换?

  • 我遇到 flock 似乎是访问文件提供的文件的锁定机制。这似乎是一个很好的解决方案,但这似乎在MacOS上不存在?

任何想法都以强大的方式解决这一要求(以便我不必每隔一天调试 - 我现在的同时发生可能会很痛苦),最受欢迎!

I need to get several processes to communicate with each others on a macOS system. These processes will be spawned several times per day at different times, and I cannot predict when they will be up at the same time (if ever). These programs are in python or swift.

How can I safely allow these programs to all write to the same file?

I have explored a few different options:

  • I thought of using sqlite3, however I couldn't find an answer in the documentation on whether it was safe to write concurrently across processes. This question is not very definitive, old, and I would ideally like to get a more authoritative answer.

  • I thought of using multiprocesing as it supports locks. However, as far as I could see in the documentation, you need a meta-process that spawns the children and stays up for the duration of the longest child process. I am am fine having a meta-spawner process, but it feels wasteful to have a meta-process basically staying up all day long, just to resolve conflicting access ?

  • Along the lines of this, I thought of having a process that stays up all day long, and receive messages from all other processes, and is the sole responsible for writing to file. It feels a little wasteful, how worried should I be about the resource cost of having a program up all day, and doing little? Are the only thing to worry about memory footprint and CPU usage (as shown in activity monitor), or could there be other significant costs, e.g. context switching?

  • I have come across flock on linux, that seems to be a locking mechanism to access files, provided by the OS. This seems like a good solution, but this does not seem to exist on macOS?

Any idea to solve this requirement in a robust manner (so that I don't have to debug every other day - I now concurrency can be a pain), is most welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渡你暖光 2025-02-13 05:44:46

当您控制所有此类过程的源代码时,您可以使用羊群。它将在文件上放置 Advisory 锁定,因此仅如果他也以相同的方式访问文件,则只有其他作者才会被阻止。如果您的流程只需要写入共享文件,这对您来说是可以的。
我已经测试过Bigsur的羊群,它仍然可以实现并正常工作。

您也可以以任何其他共同的方式进行操作:在已知位置创建临时.lock文件(这是Git的作用),并在使用主文件完成当前作者后将其删除;使用信号量; ETC

While you are in control of the source code of all such processes, you could use flock. It will put the advisory lock on file, so the other writer will be blocked only in case he is also access the file the same way. This is OK for you, if only your processes will ever need to write to the shared file.
I've tested flock on BigSur, it is still implemented and works fine.

You can also do it in any other common manner: create temporary .lock file in the known location(this is what git does), and remove it after current writer is done with the main file; use semaphores; etc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文