最近做了一个服务器的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