Python编写memcached启动脚本代码实例
程序员文章站
2022-03-16 15:17:09
memcached是一套分布式的高速缓存系统,由livejournal的brad fitzpatrick开发,但被许多网站使用。这是一套开放源代码软件,以bsd license授权发布。memcach...
memcached是一套分布式的高速缓存系统,由livejournal的brad fitzpatrick开发,但被许多网站使用。这是一套开放源代码软件,以bsd license授权发布。
memcached缺乏认证以及安全管制,这代表应该将memcached服务器放置在防火墙后。
memcached的api使用三十二比特的循环冗余校验(crc-32)计算键值后,将数据分散在不同的机器上。当表格满了以后,接下来新增的数据会以lru机制替换掉。由于memcached通常只是当作缓存系统使用,所以使用memcached的应用程序在写回较慢的系统时(像是后端的数据库)需要额外的代码更新memcached内的数据。
memcached作为缓存文件服务,默认是操作系统里面是可以直接yum -y install memcached进行安装的。
/etc/init.d/memcached 是属于系统shell编写的管理脚本,下面这个脚本是python脚本编写出来的memcached管理脚本,和shell编写的脚本实现的效果一样。
代码如下
#!/usr/bin/python import sys import os from subprocess import popen,pipe class process(object): '''memached rc script''' args = {'user':'memcached', 'port':11211, 'maxconn':1024, 'cachesize':64, 'option':''} def __init__(self,name,program,workdir): self.name = name self.program = program self.workdir = workdir def _init(self): '''/var/tmp/memcached''' if not os.path.exists(self.workdir): os.mkdir(self.workdir) os.chdir(self.workdir) def _pidfile(self): '''/var/tmp/memcached/memcached.pid''' return os.path.join(self.workdir, "%s.pid" % self.name) def _writepid(self): if self.pid: with open(self._pidfile(),'w') as fd: fd.write(str(self.pid)) def _readconf(self,f): with open(f) as fd: lines = fd.readlines() return dict([ i.strip().replace('"','').split('=') for i in lines]) def _parseargs(self): conf = self._readconf('/etc/sysconfig/memcached') if 'user' in conf: self.args['user'] = conf['user'] if 'port' in conf: self.args['port'] = conf['port'] if 'maxconn' in conf: self.args['maxconn'] = conf['maxconn'] if 'cachesize' in conf: self.args['cachesize'] = conf['cachesize'] options = ['-u',self.args['user'], '-p',self.args['port'], '-m',self.args['cachesize'], '-c',self.args['maxconn']] os.system("chown %s %s" % (self.args['user'],self.workdir)) return options def start(self): pid = self._getpid() if pid: print "%s is running..." % self.name sys.exit() self._init() cmd = [self.program] + self._parseargs() + ['-d','-p',self._pidfile()] p = popen(cmd,stdout=pipe) #self.pid = p.pid #self._writepid() print "%s start sucessful \t\t [ok]" % self.name def _getpid(self): p = popen(['pidof',self.name],stdout=pipe) pid = p.stdout.read().strip() return pid def stop(self): pid = self._getpid() if pid: os.kill(int(pid),15) if os.path.exists(self._pidfile()): os.remove(self._pidfile()) print "%s is stopped \t\t\t [ok]" % self.name def restart(self): self.stop() self.start() def status(self): pid = self._getpid() if pid: print "%s is already running" % self.name else: print "%s is not running" % self.name def help(self): print "usage:%s {start|stop|status|restart|} " % __file__ def main(): name = 'memcached' prog = '/usr/bin/memcached' args = '-u nobody -p 11211 -c 1024 -m 64' wd = '/var/tmp/memcached' pm = process(name = name, program = prog, workdir = wd) try: cmd = sys.argv[1] except indexerror,e: print "option error" sys.exit() if cmd == 'start': pm.start() elif cmd == 'stop': pm.stop() elif cmd == 'restart': pm.restart() elif cmd == 'status': pm.status() else: pm.help() if __name__ == '__main__': main()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。