- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我很好奇如何在后台运行 python 脚本,每 60 秒重复一次任务。我知道你可以使用 & 在后台添加一些东西,这对这种情况有效吗?
我正在考虑做一个循环,让它等待 60 秒并再次加载,但感觉有些不对劲。
最佳答案
与其编写自己的守护进程,不如使用 python-daemon反而! python-daemon实现 PEP 3143 的良好守护程序规范, “标准守护进程库”。
我已经包含了基于该问题的公认答案的示例代码,尽管代码看起来几乎相同,但它有一个重要的根本区别。没有 python-daemon您必须使用 &
将您的进程置于后台并使用 nohup
并防止您的进程在退出 shell 时被杀死。相反,它会在您运行程序时自动与您的终端分离。
例如:
import daemon
import time
def do_something():
while True:
with open("/tmp/current_time.txt", "w") as f:
f.write("The time is now " + time.ctime())
time.sleep(5)
def run():
with daemon.DaemonContext():
do_something()
if __name__ == "__main__":
run()
实际运行它:
python background_test.py
注意这里没有&
。
另外,this other stackoverflow answer详细解释使用 python-daemon 的诸多好处.
关于python - 高效的 Python 守护进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4637420/
一 点睛 线程可以设置为守护线程,ThreadGroup 也可以设置为守护 ThreadGroup,但是若将一个 ThreadGroup 设置为 deamon,也并不会影响线程的 daemon 属性,
我有一个 python 脚本需要在启动时作为守护进程运行。进程从 tty(和 pdb)分离,但代码不运行。 我已经将它缩小到一个最小的例子 import daemon from time import
reactjs isMounted API 的文档提到: You can use this method to guard asynchronous calls to setState() or fo
我正在开发一个需要嵌入 HTTP 服务器的守护进程。我正在尝试使用 BaseHTTPServer 来完成它,当我在前台运行它时,它工作正常,但是当我尝试将守护进程 fork 到后台时,它停止工作。我的
我正在尝试使用 Apache Commons Daemon 使用 Daemon 接口(interface)来守护我的应用程序。 Java 应用程序本身不执行任何操作,只是写入 stout。 我编译了j
我正在使用 Bootle Python Web Framework 在 Ubuntu 上开发网络应用程序。是否有任何有效的方法来守护启动默认 bottlepy 网络服务器的脚本? 谢谢。 UPD:现在
我一直使用 bluepill成功地守护简单的 Ruby 脚本。然而这一次,我有一个脚本,它也在加载 Rails 环境,因此我可以访问 Rails 应用程序及其各自模型的数据库连接。我使用的 bluep
我试图守护一些代码,但我遇到了一些麻烦。 如果我用 tklogger() 调用代码,它运行得很好。但是,如果我在守护程序上下文中调用它,我会得到以下跟踪信息: Traceback (most rece
我打算使用 systemd 将 celery 4.3.0 作为守护进程运行,但它给了我这个错误: 它会启动 worker 但会很快停止它们。但是,我可以通过键入以下命令手动运行工作人员: celery
我是一名优秀的程序员,十分优秀!