我很好奇你如何在后台运行python脚本,每60秒重复一次任务.我知道你可以使用&在后台放置一些东西,对这种情况有效吗?
我正在考虑做一个循环,让它等待60秒并重新加载它,但有些事情让人感觉不舒服.
acu*_*ich 99
而不是编写自己的守护进程,而是使用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()
Run Code Online (Sandbox Code Playgroud)
要实际运行它:
python background_test.py
Run Code Online (Sandbox Code Playgroud)
请注意&这里没有.
此外,这个其他stackoverflow答案详细解释了使用python-daemon的许多好处.
我认为你的想法几乎就是你想要的.例如:
import time
def do_something():
with open("/tmp/current_time.txt", "w") as f:
f.write("The time is now " + time.ctime())
def run():
while True:
time.sleep(60)
do_something()
if __name__ == "__main__":
run()
Run Code Online (Sandbox Code Playgroud)
通话time.sleep(60)将使您的程序进入睡眠状态60秒.当该时间结束时,操作系统将唤醒您的程序并运行该do_something()功能,然后将其恢复进入睡眠状态.当你的程序正在睡觉时,它没有什么效果.这是编写后台服务的一般模式.
要从命令行实际运行此命令,您可以使用&:
$ python background_test.py &
Run Code Online (Sandbox Code Playgroud)
执行此操作时,脚本中的任何输出都将转到与您启动它相同的终端.您可以重定向输出以避免这种情况:
$ python background_test.py >stdout.txt 2>stderr.txt &
Run Code Online (Sandbox Code Playgroud)
在Greg中描述,在shell中使用&可能是最简单的方法.
如果你真的想要创建一个强大的守护进程,你需要查看os.fork()命令.
维基百科的例子:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, time
def createDaemon():
"""
This function create a service/Daemon that will execute a det. task
"""
try:
# Store the Fork PID
pid = os.fork()
if pid > 0:
print 'PID: %d' % pid
os._exit(0)
except OSError, error:
print 'Unable to fork. Error: %d (%s)' % (error.errno, error.strerror)
os._exit(1)
doTask()
def doTask():
"""
This function create a task that will be a daemon
"""
# Open the file in write mode
file = open('/tmp/tarefa.log', 'w')
# Start the write
while True:
print >> file, time.ctime()
file.flush()
time.sleep(2)
# Close the file
file.close()
if __name__ == '__main__':
# Create the Daemon
createDaemon()
Run Code Online (Sandbox Code Playgroud)
然后你可以在doTask()块内放置你需要的任何任务.
您不需要使用&启动它,它将允许您进一步自定义执行.
| 归档时间: |
|
| 查看次数: |
49955 次 |
| 最近记录: |