python 做守护进程,实时检测队列,移动文件

本文介绍了一个使用Python实现的脚本,该脚本能够实时检测Redis数据库中的队列,当队列存在时,会将其文件移动到指定的文件夹下。通过创建守护进程常驻服务器,每隔一秒检测一次Redis服务器状态,并在有队列数据时启动线程执行文件移动操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  最近做了一个服务器的python脚本,目标,实时检测redis数据库中,的队列,若有队列存在,则将队列中的文件移动到指定文件夹下。

流程很简单,过程可费了我不少劲。

  首先创建一个主进程作为守护进程,来常驻服务器,详见代码中的f_fork () 方法,。然后每隔1秒去检测一次 redis 服务器中的队列数据,详见代码中main_loop() 和get_redis(), 若有队列存在 ,则返回队列数据,同时根据key 删除队列文件。 然后新建一条线程,来做移动文件操作。 文件操作在线程类中的重写 run方法中。贴上源码~~ 请多指教

 

  1 # -*- coding: utf-8 -*-
  2 import threading 
  3 import sys,os
  4 import time 
  5 import redis
  6 import shutil
  7 
  8 # The mythread class is derived from the class threading.Thread 
  9 class mythread (threading.Thread) : 
 10 def __init__ (self, file_list) : 
 11 threading.Thread.__init__(self) 
 12 self.thread_stop = False 
 13 self.file_list = file_list
 14 
 15 # overwrite run() method, put what you want the thread do here 
 16 def run (self) : 
 17 #while not self.thread_stop:
 18 self.cpoy_file(self.file_list) 
 19 
 20 # stop this thread
 21 def stop(self): 
 22 self.thread_stop = True 
 23 
 24 # move the file as the list says
 25 def cpoy_file (self, file_list) :
 26 # config dir
 27 new_dir = '/www/'
 28 if len(file_list) :
 29 for path in file_list :
 30 
 31 #get fileName
 32 path_list = path.split('/')
 33 filename = path_list[-1]
 34 
 35 new_path_list = path_list
 36 
 37 del new_path_list[0:2]
 38 del new_path_list[-1]
 39 new_path_string = "/".join(new_path_list)
 40 
 41 new_path_dir = new_dir + new_path_string
 42 
 43 new_file_path = new_path_dir + '/' + filename
 44 
 45 
 46 # 
 47 if not os.path.exists(new_path_dir) :
 48 try :
 49 os.mkdir(new_path_dir)
 50 except Exception as er:
 51 print '\r\n action: makedir \n time :',time.asctime(), '\n result: ', er, ' !!(',path,'-->',new_file_path,')'
 52 
 53 try :
 54 shutil.copyfile(path, new_file_path)
 55 os.remove(path) 
 56 # if do this print it
 57 # print '\r\n action: copy \n time :',time.asctime(), '\n result: SUSCESS!!(',path,'-->',new_file_path,')'
 58 except Exception as error :
 59 print '\n action: copy \n time :',time.asctime(), '\n result: FAILED!! (',path,'-->',new_file_path,')'
 60 
 61 
 62 #--create a new thread to do the move function--#
 63 def new_thread (name, file_list) :
 64 name = mythread(file_list) 
 65 name.start() 
 66 name.stop()
 67 return 
 68 
 69 
 70 # get info from redis
 71 def get_redis () :
 72 # link to redis server
 73   r = redis.Redis(host='hostname', port=6379)
 74 
 75 # get redis , hgetall . return a dictionary
 76   list_dic = r.hgetall('hkey')
 77 
 78 # loop to delete the data in redis 
 79 for key in list_dic :
 80   r.hdel('hkey', key)
 81 
 82 return list_dic 
 83 
 84 
 85 # main loop
 86 def main_loop () :
 87 while True :
 88 #for i in range(0,1):
 89 list_dic = get_redis()
 90 file_list = []
 91 
 92 # if exist list_dic change dic to list for args
 93 if len(list_dic) :
 94 # to create a list 
 95 for key in list_dic :
 96 file_list.append( list_dic[key] )
 97 
 98 # start thread threadname = now time
 99 thread_name = time.time()
100 new_thread(thread_name, file_list)
101 
102 # print '\n\r',file_list
103 time.sleep(1)
104 
105 
106 # create a fork in system forever 
107 def f_fork () :
108 # do the UNIX double-fork magic, see Stevens' "Advanced 
109 # Programming in the UNIX Environment" for details (ISBN 0201563177) 
110 try :
111 pid = os.fork()
112 if pid > 0:
113 sys.exit(0)
114 except OSError, e:
115 sys.exit(1)
116 
117 os.chdir("/") 
118 os.setsid()
119 os.umask(0)
120 
121 #second fork
122 try:
123 pid = os.fork()
124 if pid > 0 :
125 sys.exit(0)
126 except OSError, e:
127 sys.exit(1)
128 
129 
130 # enter main
131 if __name__ == '__main__': 
132 f_fork() # create main fork
133 main_loop() # enter loop 

 



转载于:https://www.cnblogs.com/lunsa/archive/2012/11/13/Python_%e5%ae%88%e6%8a%a4%e7%a8%8b%e5%ba%8f_%e6%96%87%e4%bb%b6%e6%93%8d%e4%bd%9c.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值