python扭曲INotify而不阻塞反应堆

发布于 2024-11-11 14:56:57 字数 447 浏览 6 评论 0原文

我正在使用 twsited 的 INotify 来监视 /dev 目录以监视添加的新串行设备。我当前使用的代码类似于下面的代码。

notifier = INotify()
notifier.watch(FilePath("/dev"), IN_CREATE, callbacks=[self.created])
notifier.startReading()

def created(self, ignored, path, mask):
    ...
    blocking code
    ...

我目前遇到的问题是,当调用“created”时,它会阻塞我的反应器,因此其他网络会话(我有与同一反应器关联的 TCP 和 UDP 连接)必须等待“created”方法结束。

有谁知道如何使“创建”方法在后台运行,这样它就不会阻塞我的反应器?

谢谢,

西蒙

I am using twsited's INotify to monitor the /dev directory to monitor for new serial devices being added. The code I am currently using is similar to below.

notifier = INotify()
notifier.watch(FilePath("/dev"), IN_CREATE, callbacks=[self.created])
notifier.startReading()

def created(self, ignored, path, mask):
    ...
    blocking code
    ...

The problem I am having at the moment is when 'created' is getting called, it is blocking my reactor so other network sessions (I have both TCP and UDP connections associated with the same reactor) have to wait for the 'created' method to finish.

Does anyone know how I can make the 'created' method run in the background so it doesn't block my reactor?

Thanks,

Simon

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

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

发布评论

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

评论(2

街角卖回忆 2024-11-18 14:56:57

Twisted 中的所有事件处理程序都在“反应器线程”中运行 - UDP、TCP,实际上还有 inotify。他们都应该与系统合作,不进行阻止。所以,从这个意义上说,这只是一个关于如何在 Twisted 中编写好的事件处理程序的问题,而不是特别关于 inotify 的问题。

有很多避免阻塞的选项。回答您的问题的棘手之处在于,正确的选项取决于当前代码块的确切原因。

它做套接字 I/O 吗?使用 Twisted 非阻塞 套接字 I/O API 代替。

它执行文件系统 I/O 吗?您可能需要在这里使用线程,因为非阻塞文件系统如果没有 I/O,I/O 就会很困难(也许并非不可能)。

它与 SQL 数据库对话吗?也许 twisted.enterprise.adbapi 可以提供帮助。

等等。

我不知道这是否涵盖了您遇到的情况。但是,我会强调两件事。首先,在 Twisted 程序中使用线程是完全合理的。 Twisted 的大部分内容都存在,因此您不必使用线程,但如果您遇到线程完成工作而其他任何事情都完成的情况 - 那就使用它(小心;)。 Twisted 甚至还有一些帮助器来使其变得更容易,例如 zeekay 提到的 deferToThread。其次,为任务选择合适的解决方案。所有“阻塞”问题的集合仅比所有一般编程问题的集合略小。有很多可能的解决方案。有些(例如线程)似乎具有广泛的适用性,但稍加注意,您可能会发现更适合特定情况的东西。

此外,请查看Twisted:使代码成为非阻塞以获取更多说明。

All event handlers in Twisted run in the "reactor thread" - UDP, TCP, and indeed inotify. They all are expected to cooperate with the system by not blocking. So, in this sense, this is just a question about how to write good event handlers in Twisted, not about inotify in particular.

There are lots of options for avoiding blocking. The tricky thing about answering your question is that the right option depends on why exactly the current code blocks.

Does it do socket I/O? Use Twisted's non-blocking socket I/O APIs instead.

Does it do filesystem I/O? You may need to use a thread here, since non-blocking filesystem I/O is difficult (perhaps not impossible) without one.

Does it talk to a SQL database? Perhaps twisted.enterprise.adbapi can help.

And so on.

I don't know if that covers the case you're in. However, I'll emphasize two things. First, it is perfectly reasonable to use threads in a Twisted program. Much of Twisted exists so you won't have to use threads, but if you come to a circumstance where threads get the job done and nothing else does - go for it (with caution ;). Twisted even has some helpers to make it easier, such as the deferToThread mentioned by zeekay. Second, pick the appropriate solution for the task. The collection of all "blocking" problems is only slightly smaller than the collection of all general programming problems. There are lots of possible solutions. Some, like threads, seem to have a wide range of applicability, but with a little care you might find something more specifically suitable to a particular circumstance.

Additionally, take a look at Twisted: Making code non-blocking for some further explanation.

鹿童谣 2024-11-18 14:56:57

您可以使用twisted.internet.threads.deferToThread在线程中运行阻塞代码:

deferToThread(self.created, ignored, path mask)

You could use twisted.internet.threads.deferToThread to run your blocking code in a thread:

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