如何优化python程序以处理多个更新的文件?

2024-09-22 16:30:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python程序,它在目标位置创建一些文件的键和值对,其中文件名为key,生成的散列键用作值。我正在使用md5checksum检测文件中的更改。因此,当任何文件的内容发生任何更改时,我的程序将检测修改后的文件并执行子流程模块中提到的某些CLI命令。下面是该程序的完整片段

import glob
import hashlib
import copy
import subprocess
import time

license_file = list(glob.iglob('<path_to>/*.lic'))

license_hashkey = dict()


def generate_md5checksum(license_file, ex="", inc=""):
    hash_obj = hashlib.md5()
    try:
        fd = open(license_file,"rb")
    except IOError:
        print("Can't retrieve MD5sum for ", license_file)
        pass
    content = fd.readlines()
    fd.close()
    for eachLine in content:
        if ex and eachLine.startswith(ex):
            continue
        hash_obj.update(eachLine)
    hash_obj.update(inc.encode('utf-8'))
    return hash_obj.hexdigest()

for key_value in license_file:   
    license_hashkey[key_value] = generate_md5checksum(key_value)

'''
Below program that compare the two copies of the dictionary and execute the lmreread on the license file that was updated with the new content.
'''    
compare_license_key = copy.deepcopy(license_hashkey)
while True:
    for keys in license_hashkey:
        if license_hashkey[keys] != compare_license_key[keys]:
            updated_license = list(license_hashkey.keys())[list(license_hashkey.values()).index(license_hashkey[keys])]
            print("license hash key: " + license_hashkey[keys] +  "  compare license key: " + compare_license_key[keys])
            subprocess.Popen(['lmutil', 'lmreread', '-c', updated_license])
            time.sleep(12)
            compare_license_key = copy.deepcopy(license_hashkey)
        else:
            print("hold tight, looking for a new or an updated license file")
        time.sleep(1)
    for key_value in license_file:
        license_hashkey[key_value] = generate_md5checksum(key_value)

这个程序非常有效。但是,我需要优化程序,以便即使多个用户在给定位置修改不同的文件,我的代码也会检测到它们,并可能会创建这些文件的列表或字典,并在代码处理完一个请求后立即执行CLI命令。目前,我的代码将处理一个请求,但是在处理这一个请求时,它将无法捕获其他修改过的文件(如果有的话)。 有一种方法我尝试如下

1) Declare an empty list out of the "while" loop. 
2) Run a "for" loop over the "updated_license" from my program inside the "while" loop.
3) Append the new list under the for loop in the point 2. 
4) Run another "for" loop over the new list outside the "for" loop that is declare just below the "while" loop.

下面是优化的方法,但它不起作用

#lic_list = []
while True:
    for keys in md5s:
        if md5s[keys] != temp[keys]:
            print("change detected")

            license_file = list(md5s.keys())[list(md5s.values()).index(md5s[keys])]
            #lic_list = [new for new in license_file]
            #lic_list.append(lic_list)            
            subprocess.Popen(['lmutil', 'lmreread', '-c', license_file])
            temp = copy.deepcopy(md5s)
        else:
            print("no change")
        time.sleep(1)
    #for i in lic_list:
     #   subprocess.Popen(['lmutil', 'lmreread', '-c', i])
    for fname in file:
        md5s[fname] = getmd5(fname)
      #  temp = copy.deepcopy(md5s)
       # lic_list = []

注意:我已经散列了我用来优化程序的行


Tags: 文件thekeyin程序loopforvalue