在NoFlo中,我经常遇到这样的组件:
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等待main到来并接收sup1和sup2数据包,但随后下一个sup1数据包到达,该数据包应该与下一个main结合,同时仍在等待前一个main到来。其结果将是更多或更高的数据吞吐量上的完全混乱。
NoFlo异步组件是否有任何数据竞争保护手段,还是完全由组件设计师决定?
这里有两个方面的问题:同步输入和维护内部状态。内部状态在这里或多或少受到保护,因为Node.js不是多线程的,并且在前一个doAsync()处理程序完成之前,不会尝试访问状态变量。但是同步输入仍然是一个问题。
发布于 2014-05-21 01:50:25
事实证明,对于NoFlo v0.5.1,没有针对数据竞争的内置辅助工具,组件设计人员必须自己关心它。
对于异步组件,这意味着:
发布于 2014-11-25 01:45:33
对于最新版本的NoFlo,推荐的方式是使用noflo.helpers.WirePattern,并使用组进行同步。
https://stackoverflow.com/questions/23649753
复制相似问题