Python和Asyncio带有iNotify奇怪的问题

发布于 2025-01-30 13:07:44 字数 1271 浏览 5 评论 0原文

当将Python Asyncio与Inotify库中使用Python Asyncio时,我遇到了一个奇怪的问题。我尝试过异步化和牛头怪第三方图书馆,都给了我同样的问题。我创建了一个简单的示例,证明了这个问题:

import asyncio
from asyncinotify import Inotify, Mask
from contextlib import suppress


async def watcher_task():
    watchdir = "/home/user/test/"
    
    with Inotify() as n:
        n.add_watch(watchdir, Mask.CLOSE_WRITE)
        async for event in n:
            print(event)
            
                       
async def main():
    task = asyncio.create_task(watcher_task())
    print('running task for 20 seconds')
    await asyncio.sleep(20)
    
    # Creating files in the watchdir will now show/print
    # the event happening in watcher_task(). No issues
    
    print('cancelling task')
    task.cancel()
    with suppress(asyncio.CancelledError):
        await task
        
    print('creating task again')
    task = asyncio.create_task(watcher_task())
    print('running task for 20 seconds')
    
    # After cancelling the task, and recreating it,
    # no events are triggered/printed now in watcher_task
    # when files are created in the watchdir... WHY!?
    
    await asyncio.sleep(20)
       
       
asyncio.run(main())

我是否有明显的东西?为什么仅在创建任务而不是随后的时间时仅在工作时进行进程?

I'm having a weird issue when using Python asyncio with Inotify libraries. I have tried both asyncinotify and minotaur third party libraries and both give me the same issue. I have created a simple example that demonstrates the issue:

import asyncio
from asyncinotify import Inotify, Mask
from contextlib import suppress


async def watcher_task():
    watchdir = "/home/user/test/"
    
    with Inotify() as n:
        n.add_watch(watchdir, Mask.CLOSE_WRITE)
        async for event in n:
            print(event)
            
                       
async def main():
    task = asyncio.create_task(watcher_task())
    print('running task for 20 seconds')
    await asyncio.sleep(20)
    
    # Creating files in the watchdir will now show/print
    # the event happening in watcher_task(). No issues
    
    print('cancelling task')
    task.cancel()
    with suppress(asyncio.CancelledError):
        await task
        
    print('creating task again')
    task = asyncio.create_task(watcher_task())
    print('running task for 20 seconds')
    
    # After cancelling the task, and recreating it,
    # no events are triggered/printed now in watcher_task
    # when files are created in the watchdir... WHY!?
    
    await asyncio.sleep(20)
       
       
asyncio.run(main())

Is there something obvious I'm missing? Why does Inotify only work the first time the task is created, and not subsequent times?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪心 2025-02-06 13:07:44

我是Minotaur的作者,我可以重现此错误,这是一个错误。修复在v0.1.0中

I am author of minotaur, I can reproduce this error and it is a bug. A fix is in v0.1.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文