且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在python Django框架中仅强制执行一个正在运行的进程实例?

更新时间:2022-06-22 04:24:34

正如KlausD在他的评论中提到的,规范的(与语言无关的)解决方案是使用包含正在运行的进程的pid的锁定文件,因此代码负责锁获取的可以检查进程是否仍在运行.

As KlausD mentions in his comment, the canonical (and language-agnostic) solution is to use a lock file containing the pid of the running process, so the code responsible for the lock acquisition can check if the process is still running.

如果您在项目中使用Redis,另一种解决方案是将锁存储在Redis中,其TTL比任务的最坏情况下的运行时间更长.这样可以确保将锁释放出来,并且还可以根据需要轻松地在多个服务器之间共享锁.

An alternative solution if you use redis in your project is to store the lock in redis with a TTL that's a bit longer than the worst case runtime of the task. This makes sure the lock will be freed whatever, and also allow to easily share the lock between multiple servers if needed.

该进程是否有可能崩溃,而另一个进程获取了相同的pid?

is it possible that the process crashes and another process pick up the same pid?

,甚至是获取进程统计信息检查进程的开始时间,命令行,父进程等,并确定是同一进程还是新进程的可能性.

Yes, of course, and that's even rather likely (and this is an understatement) on a server running for month or more without reboot, and even more so if the server runs a lot of short-lived processes. You will not only have to check if there's a running process matching this pid but also get the process stats to inspect the process start time, the command line, the parent etc and decides the likelyhood it's the same process or a new one.

请注意,这并不是什么新鲜事物-大多数过程监控工具都面临相同的问题,因此您可能需要检查一下它们是如何解决的(在这里,独角兽可能是一个很好的起点).

Note that this is nothing new - most process monitoring tools face the same problem, so you may want to check how they solved it (gunicorn might be a good starting point here).