我正在使用监视程序来查看按时间间隔通过 ftplib 下载的新 .xml 文件的目录。当Watchdog看到该文件时, on_created()会触发一个处理/解析xml的函数,但似乎文件下载尚未完成,但在后续函数中导致数据丢失。
我在调用函数之前添加了 time.sleep(1),这缓解了错误,但在现实世界中添加延迟似乎是一种不可靠的方法。我想知道是否有类似于承诺功能的方法我可以使用而不是延迟。或者我可能完全误解了这个问题并且有一个简单的答案?接受任何建议。
仅供参考...文件大小可以从大约100K到4-5mg不等。
def download(f):
ftpt = ftplib.FTP(server)
ftpt.login(username, password)
ftpt.cwd(ftp_dir)
print 'Connected to FTP directory'
if f.startswith('TLC-EMAILUPDATE'):
if os.path.exists(dl_dir + f) == 0:
fhandle = open(os.path.join(dl_dir, f), 'wb')
print 'Getting ' + f
ftpt.retrbinary('RETR ' + f, fhandle.write)
fhandle.close()
elif os.path.exists(dl_dir + f) == 1:
print 'File', f, 'Already Exists, Skipping Download'
ftp = ftplib.FTP(server)
ftp.login(username, password)
ftp.cwd(ftp_dir)
infiles = ftp.nlst()
pool = Pool(4)
pool.map(download, in files)
def on_created(self, event):
self.processfile(event)
base = os.path.basename(event.src_path)
if base.startswith('TLC-EMAILUPDATE'):
print 'File for load report has been flagged'
xmldoc = event.src_path
if os.path.isfile(xmldoc) == 1:
print 'File download complete'
send_email(xmldoc)
在 content 变量处抛出异常,其中解析无法从下载的文件中读取任何数据。
def send_email(xmldoc):
time.sleep(2)
content = str(parse_xml.create_template(xmldoc))
msg = MIMEText(content, TEXT_SUBTYPE)
msg['Subject'] = EMAIL_SUBJECT
msg['From'] = EMAIL_SENDER
msg['To'] = listToStr(EMAIL_RECEIVERS)
try:
smtpObj = SMTP(GMAIL_SMTP, GMAIL_SMTP_PORT)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.ehlo()
smtpObj.login(user=EMAIL_SENDER, password=EMAIL_PASS)
smtpObj.sendmail(EMAIL_SENDER, EMAIL_RECEIVERS, msg.as_string())
smtpObj.quit()
print 'Email has been sent to %s' % EMAIL_RECEIVERS
except SMTPException as error:
print 'Error: unable to send email : {err}'.format(err=error)
答案 0 :(得分:1)
简单回答:切换到监控CLOSE_WRITE
事件。唉Watchdog并不直接支持它。之一:
1)切换到pyinotify并使用以下代码 - 仅限Linux,而不是OSX
2)将Watchdog与on_any_event()
import os, sys
import pyinotify
class VideoComplete(pyinotify.ProcessEvent):
def process_IN_CLOSE_WRITE(self, event):
sys.stdout.write(
'video complete: {}\n'.format(event.pathname)
)
sys.stdout.flush()
def main():
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(
wm, default_proc_fun=VideoComplete(),
)
mask = pyinotify.ALL_EVENTS
path = os.path.expanduser('~/Downloads/incoming')
wm.add_watch(path, mask, rec=True, auto_add=True)
notifier.loop()
if __name__=='__main__':
main()
echo beer > ~/Downloads/incoming/beer.txt
video complete: /home/johnm/Downloads/incoming/beer.txt