Rust 中是否有像 NodeJS 的 EventEmitter 这样的功能? [英] Is there a feature like NodeJS's EventEmitter in Rust?

查看:114
本文介绍了Rust 中是否有像 NodeJS 的 EventEmitter 这样的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个中央对象",多个任务可以订阅"异步更新消息.

I'm looking for a central 'object' to which multiple tasks can 'subscribe' for async update messages.

推荐答案

据我了解,EventEmitter 只是一个支持事件监听器的通用接口;实现"此接口的对象提供了几种事件,客户端代码可以在这些事件上放置侦听器回调.当对象自行决定发出相应的事件时,将调用这些回调.由于 JS 是动态类型语言,这样的接口很自然地出现,并且可以通过很多东西来实现.

As far as I understand, EventEmitter is just a generic interface for event listeners support; objects which "implement" this interface provide several kinds of events on which the client code can put listener callbacks. These callbacks will be then called when corresponding event is emitted on the object's discretion. As JS is dynamically typed language, such interface arise very naturally and can be implemented by a lot of things.

首先,无论是在 NodeJS 中还是在 Rust 中,您都不能订阅"任务/线程:您在某个对象上放置了一个侦听器回调,然后该回调将从某个线程(甚至可能是当前线程)中调用,但是在一般订阅对象的线程和运行此回调的线程是不同的.在 NodeJS 中有一个全局事件循环,它调用函数和外部事件侦听器,它们又可以调用其他事件侦听器,因此您并不真正知道哪个线程将执行侦听器.并不是您应该关心 - 事件循环抽象对您隐藏了显式线程.

First of all, neither in NodeJS nor in Rust you can "subscribe" tasks/threads: you put a listener callback on some object, and then this callback will be invoked from some thread, possibly even the current one, but in general the thread which subscribes to an object and the thread in which this callback will be run are different. In NodeJS there is a global event loop which calls into functions and external event listeners which in turn can invoke other event listeners, so you don't really know which thread will execute the listener. Not that you should care - event loop abstraction hides explicit threading from you.

然而,Rust 是一种合适的多线程语言.它不会在全局事件循环上运行(尽管通过 libgreen 有可能 - 但 - 在类似于 Node 中的事件循环中运行 Rust 程序;它将用于任务管理和我/O 处理,但在不久的将来它将与 libstd 分离).默认的 Rust 运行时,libnative,公开了用于创建原生抢占式调度线程和原生 I/O 的工具.这意味着哪个线程最终执行回调并不重要,并且您应该记住,所有回调都将在拥有该对象的线程中执行,除非它专门为事件处理创建单独的线程.

Rust, however, is a proper multithreaded language. It does not run over a global event loop (though via libgreen it is possible - yet - to run Rust program in an event loop similar to the one in Node; it will be used for task management and I/O handling, but it will be separated from the libstd in the near future). Default Rust runtime, libnative, exposes facilities for creating native preemptively-scheduled threads and native I/O. This means that it does matter which thread eventually executes the callback, and you should keep in mind that all callbacks will be executed in the thread which owns the object, unless it creates separate threads specifically for event handling.

侦听器的另一类问题是 Rust 是静态类型语言,并且在静态类型语言中编写通用事件侦听器接口比在动态类型语言中要困难一些,因为您需要编写一个足够多态的接口.您还希望利用强类型系统并使您的界面尽可能类型安全.这不是一项微不足道的任务.当然,可以在任何地方使用 Box,但这样的 API 使用起来不会很愉快.

Another kind of problem with listeners is that Rust is statically typed language, and writing generic event listener interface is somewhat more difficult in statically typed languages than in dynamically typed ones, because you will need to write a sufficiently polymorphic interface. You would also want to take advantage of strong type system and make your interface as type safe as possible. This is not a trivial task. Sure, it is possible to use Box<Any> anywhere, but such an API wouldn't be very pleasant to work with.

所以,目前还没有通用的事件监听器接口.也没有事件总线库.但是,您始终可以自己编写一些东西.如果不是很通用,写起来应该不会很困难.

So, at the moment there is no general-purpose event listener interface. There is no event bus library either. However, you can always write something yourself. If it is not very generic, it shouldn't be very difficult to write it.

这篇关于Rust 中是否有像 NodeJS 的 EventEmitter 这样的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆