我是 Flask 新手,想要确保 redis 服务器正在运行,如果没有运行则启动它。这是我所拥有的:
@app.before_first_request
def initialize():
cmd = 'src/redis-cli ping'
p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
out, err = p.communicate()
#if out.startswith('Could not connect to Redis'): #start redis here
if err is not None: raise Exception(err)
但是,我收到错误“OSError:[Errno 2]没有这样的文件或目录”
有没有更简单的方法来检查redis服务器是否正在运行?
Use https://stackoverflow.com/a/12968704/538284 ping http://redis-py.readthedocs.org/en/latest/#redis.StrictRedis.pingredis的cmd:
import redis
from redis import ConnectionError
import logging
logging.basicConfig()
logger = logging.getLogger('redis')
rs = redis.Redis("localhost")
try:
rs.ping()
except ConnectionError:
logger.error("Redis isn't running. try `/etc/init.d/redis-server restart`")
exit(0)
示例输出:
ERROR:redis:Redis isn't running. try `/etc/init.d/redis-server restart`
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)