Python在另一个进程写入时旋转日志文件
我有以下python脚本,每1秒将输出写入日志文件,如下所示:Python在另一个进程写入时旋转日志文件,python,Python,我有以下python脚本,每1秒将输出写入日志文件,如下所示: python myprocess.py >> mylog.csv def rotate_logs(): # Rotate all logs from *.csv to *.csv.1 for subdir, dirs, files in os.walk(LOG_FILE_DIRECTORY): for filename in files: file_ext = os.path.splitext(
python myprocess.py >> mylog.csv
def rotate_logs():
# Rotate all logs from *.csv to *.csv.1
for subdir, dirs, files in os.walk(LOG_FILE_DIRECTORY):
for filename in files:
file_ext = os.path.splitext(filename)[1]
if file_ext == '.1':
logger.info("Delete %s", os.path.join(subdir, filename))
os.remove(os.path.join(subdir, filename))
continue
if file_ext == '.csv':
filePath = os.path.join(subdir, filename) # get the path to your file
newFilePath = filePath.replace(".csv", ".csv.1" # create the new name
logger.info("Changing name from %s to %s", filePath, newFilePath)
os.rename(filePath, newFilePath)
我有另一个python脚本logrotate.py
,每30秒取一次mylog.csv
文件,将名称更改为mylog.csv.1
,并将其移动到另一个目录,如下所示:
python myprocess.py >> mylog.csv
def rotate_logs():
# Rotate all logs from *.csv to *.csv.1
for subdir, dirs, files in os.walk(LOG_FILE_DIRECTORY):
for filename in files:
file_ext = os.path.splitext(filename)[1]
if file_ext == '.1':
logger.info("Delete %s", os.path.join(subdir, filename))
os.remove(os.path.join(subdir, filename))
continue
if file_ext == '.csv':
filePath = os.path.join(subdir, filename) # get the path to your file
newFilePath = filePath.replace(".csv", ".csv.1" # create the new name
logger.info("Changing name from %s to %s", filePath, newFilePath)
os.rename(filePath, newFilePath)
在
logrotate.py
旋转日志文件的同时,我可能会错过myprocess.py
写入日志的情况下的日志?如果是这样,我该如何防止这种情况发生?你为什么要重新发明轮子?使用