如何在将来包装Web Worker响应消息? [英] How to wrap Web Worker response messages in futures?

查看:85
本文介绍了如何在将来包装Web Worker响应消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑一个在浏览器中运行的scala.js应用程序,该应用程序由一个主程序和一个Web工作程序组成.

Please consider a scala.js application which runs in the browser and consists of a main program and a web worker.

主线程通过传递包含方法名称和调用它们所需的参数的消息,将长时间运行的操作委派给Web Worker.工作者以响应消息的形式将方法返回值传递回主线程.

The main thread delegates long running operations to the web worker by passing messages that contain the names of methods and the parameters required to invoke them. The worker passes method return values back to the main thread in the form of response messages.

更简单地说,该程序抽象化Web工作程序消息传递,以便主线程中的代码可以使用惯用和异步Scala语法调用工作程序线程中的方法.

In simpler terms, this program abstracts web worker messaging so that code in the main thread can call methods in the worker thread in idiomatic and asynchronous Scala syntax.

由于Web工作人员不会以任何方式将消息与其响应相关联,因此抽象依赖于注册表(中介对象),该对象管理每个跨上下文方法调用以将调用与结果相关联.这个单例还可以绑定回调函数,但是有没有办法用期货代替回调来完成呢?

Because web workers do not associate messages with their responses in any way, the abstraction relies on a registry, an intermediary object, that governs each cross context method call to associate the invocation with the result. This singleton could also bind callback functions but is there a way to accomplish this with futures instead of callbacks?

如何在此注册表上构建一个抽象,使程序员可以将其与Scala中的标准异步编程结构(期货和承诺)一起使用?

How can I build an abstraction over this registry that allows programmers to use it with the standard asynchronous programming structures in Scala: futures and promises?

我应该如何编写此功能,以便Scala程序员可以以规范的方式与之交互?例如:

How should I write this functionality so that scala programmers can interact with it in the canonical way? For example:

// long running method in the web worker
val f: Future[String] = Registry.ultimateQuestion(42) // async

f onSuccess { case q => println("The ultimate question is: " + q) }

我对期货和承诺并不陌生,但是当某些执行块终止时,它们似乎通常会完成.在这种情况下,收到来自网络工作者的响应表示将来已完成.有没有一种方法可以编写将未来的完成状态委托给外部流程的自定义未来?有没有其他方法可以将网络工作者响应消息链接到未来状态?

I'm new to futures and promises, but it seems like they usually complete when some execution block terminates. In this case, receiving a response from the web worker signifies completion of the future. Is there a way to write a custom future that delegates its completion status to an external process? Is there another way to link the web worker response message to the status of the future?

我可以/应该扩展未来特征吗?Scala.js中可能吗?有没有我应该扩展的具体课程?还有其他方法可以将这些跨上下文Web Worker方法调用封装在现有的异步Scala功能中吗?

Can/Should I extend the Future trait? Is this possible in Scala.js? Is there a concrete class that I should extend? Is there some other way to encapsulate these cross context web worker method calls in existing asynchronous Scala functionality?

谢谢您的考虑.

推荐答案

嗯.只是在这里吐痰(我还没有使用过工人),但是在您正在使用的单线程JavaScript世界中,将请求与Future关联起来似乎很容易.

Hmm. Just spitballing here (I haven't used workers yet), but it seems like associating the request with the Future is fairly easy in the single-threaded JavaScript world you're working in.

这是一个假想的设计.假设对工作人员的每个请求/响应都自动包装在一个信封中;信封包含一个RequestId.因此,发送方看起来像(这是伪代码,但是真实的):

Here's a hypothetical design. Say that each request/response to the worker is automatically wrapped in an Envelope; the Envelope contains a RequestId. So the send side looks something like (this is pseudo-code, but real-ish):

def sendRequest[R](msg:Message):Future[R] = {
  val promise = Promise[R]
  val id = nextRequestId()
  val envelope = Envelope(id, msg)
  register(id, promise)
  sendToWorker(envelope)
  promise.future
}

worker处理msg,将结果包装在另一个Envelope中,结果通过类似以下的操作在主线程中处理:

The worker processes msg, wraps the result in another Envelope, and the result gets handled back in the main thread with something like:

def handleResult(resultEnv:Envelope):Unit = {
  val promise = findRegistered(resultEnv.id)
  val result = resultEnv.msg
  promise.success(result)
}

这需要一些填充,并且需要考虑像 R 这样的类型,但是这种轮廓可能效果不错.如果这是JVM,则您必须担心各种竞争情况,但是在单线程JS世界中,它可能很简单,就像使用自动递增的整数作为请求ID并存储Promise一样.

That needs some filling in, and some thought about what the types like R should be, but that sort of outline would probably work decently well. If this was the JVM you'd have to worry about all sorts of race conditions, but in the single-threaded JS world it probably can be as simple as using an autoincrementing integer for the request ID, and storing away the Promise...

这篇关于如何在将来包装Web Worker响应消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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