那麼,有更好的,特定於平臺的方式來創建文件時得到通知。 Gerrat在他的評論中將Windows鏈接到了一個,並且pyinotify
可以用於Linux。這些特定於平臺的方法可能會被插入到asyncio
中,但是最終你會寫一大堆代碼來使其以獨立於平臺的方式工作,這可能不值得花費精力來檢查單個文件。但是,如果您需要更復雜的文件系統,那麼這可能值得追求。例如,看起來pyinotify
可以調整爲將Notifier
類的子類添加到asyncio
事件循環中(例如,已有類tornado
和asyncore
)。
爲您簡單的用例,我覺得你的無限循環的方式來投票是好的,但你也可以只安排回調與事件循環,如果你想:
def watch_for_file(file_path, interval=1, loop=None):
if not loop: loop = asyncio.get_event_loop()
if not os.path.exists(file_path):
print("{} not found yet.".format(file_path))
loop.call_later(interval, watch_for_file, file_path, interval, loop)
else:
print("{} found!".format(file_path))
loop.stop()
def main(argv=None):
cli_parser = make_cli_parser()
args = cli_parser.parse_args(argv)
loop = asyncio.get_event_loop()
loop.call_soon(watch_for_file, args.file_path)
loop.run_forever()
我不知道雖然這比無限循環更優雅。
編輯:
只是爲了好玩,我寫了使用pyinotify
一個解決方案:
import pyinotify
import asyncio
import argparse
import os.path
class AsyncioNotifier(pyinotify.Notifier):
"""
Notifier subclass that plugs into the asyncio event loop.
"""
def __init__(self, watch_manager, loop, callback=None,
default_proc_fun=None, read_freq=0, threshold=0, timeout=None):
self.loop = loop
self.handle_read_callback = callback
pyinotify.Notifier.__init__(self, watch_manager, default_proc_fun, read_freq,
threshold, timeout)
loop.add_reader(self._fd, self.handle_read)
def handle_read(self, *args, **kwargs):
self.read_events()
self.process_events()
if self.handle_read_callback is not None:
self.handle_read_callback(self)
class EventHandler(pyinotify.ProcessEvent):
def my_init(self, file=None, loop=None):
if not file:
raise ValueError("file keyword argument must be provided")
self.loop = loop if loop else asyncio.get_event_loop()
self.filename = file
def process_IN_CREATE(self, event):
print("Creating:", event.pathname)
if os.path.basename(event.pathname) == self.filename:
print("Found it!")
self.loop.stop()
def make_cli_parser():
cli_parser = argparse.ArgumentParser(description=__doc__)
cli_parser.add_argument('file_path')
return cli_parser
def main(argv=None):
cli_parser = make_cli_parser()
args = cli_parser.parse_args(argv)
loop = asyncio.get_event_loop()
# set up pyinotify stuff
wm = pyinotify.WatchManager()
mask = pyinotify.IN_CREATE # watched events
dir_, filename = os.path.split(args.file_path)
if not dir_:
dir_ = "."
wm.add_watch(dir_, mask)
handler = EventHandler(file=filename, loop=loop)
notifier = pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=handler)
loop.run_forever()
if __name__ == '__main__':
main()
如果你沒有使用asyncio設置滾動自己,你可以看看[在這個相關的問題](http://stackoverflow.com/questions/182197/how-do-i-watch-a- file-for-changes-using-python)用於其他可能的解決方案。 – Gerrat 2014-10-16 21:39:02