Asynchronous NoFlo异步组件和数据竞争
在NoFlo中,我经常遇到这样的组件:Asynchronous NoFlo异步组件和数据竞争,asynchronous,noflo,Asynchronous,Noflo,在NoFlo中,我经常遇到这样的组件: noflo = require 'noflo' class Foo extends noflo.AsyncComponent constructor: -> @inPorts = new noflo.InPorts main: datatype: 'int' description: 'Main async input' required: true sup1:
noflo = require 'noflo'
class Foo extends noflo.AsyncComponent
constructor: ->
@inPorts = new noflo.InPorts
main:
datatype: 'int'
description: 'Main async input'
required: true
sup1:
datatype: 'string'
description: 'Supplementary input #1'
required: true
sup2:
datatype: 'int'
description: 'Supplementary input #2'
required: true
@outPorts = new noflo.OutPorts
out:
datatype: 'object'
description: 'Result object'
error:
datatype: 'object'
@sup1 = null
@sup2 = null
@inPorts.sup1.on 'data', (@sup1) =>
@inPorts.sup2.on 'data', (@sup2) =>
super 'main', 'out'
doAsync: (main, callback) ->
unless @sup1 and @sup2
return callback new Error "Supplementary data missing"
# Combine data received from different sources
result =
main: main
sup1: @sup1
sup2: @sup2
# Reset state until next iteration
@sup1 = null
@sup2 = null
# Send the result
@outPorts.out.send result
@outPorts.out.disconnect()
callback()
exports.getComponent = -> new Foo
它假设所有3个输入连接都以某种方式同步,尽管网络主要由异步组件组成。考虑这种情况:<代码> FoO 等待<代码>主< /代码>来接收<代码> SUP1和<代码> SUP2包,但是接下来的<代码> SUP1分组到达,应该与下一个代码>主< /代码>相结合,同时仍在等待以前的<代码>主<代码>。其结果将是在更高或更高的数据吞吐量上造成完全混乱
NoFlo异步组件是否有任何数据竞争保护方法,或者完全由组件设计者决定
这里的问题有两个方面:同步输入和维护内部状态。内部状态在这里或多或少受到保护,因为Node.js不是多线程的,在上一个
doAsync()
处理程序完成之前,不会有任何内容尝试访问状态变量。但是同步输入仍然是一个问题。正如NoFlo v0.5.1所证明的那样,没有针对数据竞争的内置辅助工具,组件设计者必须自己关心它
对于异步组件,这意味着:
对于最新版本的NoFlo,建议使用NoFlo.helpers.WirePattern,并使用组进行同步