got*_*nes 11 python python-3.x python-asyncio
我正在尝试使用Python的asyncio库来确定一种观察文件外观的好方法.这是我到目前为止所提出的:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Watches for the appearance of a file."""
import argparse
import asyncio
import os.path
@asyncio.coroutine
def watch_for_file(file_path, interval=1):
while True:
if not os.path.exists(file_path):
print("{} not found yet.".format(file_path))
yield from asyncio.sleep(interval)
else:
print("{} found!".format(file_path))
break
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()
loop.run_until_complete(watch_for_file(args.file_path))
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
我把它保存为watch_for_file.py,并可以运行它
python3 watch_for_file.py testfile
Run Code Online (Sandbox Code Playgroud)
在另一个shell会话中,我发布
touch testfile
Run Code Online (Sandbox Code Playgroud)
结束循环.
有没有比使用这个无限循环更优雅的解决方案yield from asyncio.sleep()?
嗯,有一些更好的,特定于平台的方式在创建文件时得到通知.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()
Run Code Online (Sandbox Code Playgroud)
不过,我不确定这比无限循环更优雅.
编辑:
只是为了好玩,我写了一个解决方案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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4644 次 |
| 最近记录: |