3

此处列出的代码将给出部分输出,然后是错误 (self.htmlparser.parseChunk)。当使用 async.series 而不是 async.parallel 时,此示例按预期工作

ping 网络服务将等待 2 秒,然后输出“pong”,以模拟网络服务调用

应用程序.coffee

async = require 'async'
start = (new Date()).getTime()

require('node.io').scrape () ->
    @ping = (callback, n) =>
            @getHtml 'http://localhost:8888/ping', (err, $, data) => 
                diff = (new Date()).getTime() - start
                console.log "#{n} : #{diff}"
                callback err, data
    async.parallel [
        (callback) =>
            @ping callback, 1
        ,
        (callback) =>
            @ping callback, 2
        ,
        (callback) =>
            @ping callback, 3
        ,
        ], (err,results) =>
            @exit err if err?
            console.log n for n in results
            @emit 'done'

使用 async.series 输出

1 : 2079
2 : 4089
3 : 6093
1
2
3
done
OK: Job complete

使用 async.parallel 输出

3 : 2079
/home/nodeuser/src/nodews/client/node_modules/node.io/lib/node.io/request.js:296
                    self.htmlparser.parseChunk(chunk);
TypeError: Cannot call method 'parseChunk' of null

系统信息

nodeuser@ubuntu:~/src/nodews/client$ node -v && coffee -v && npm -v
v0.4.12
CoffeeScript version 1.1.3
1.0.106

nodeuser@ubuntu:~/src/nodews/client$ uname -a
Linux ubuntu 2.6.38-12-generic #51-Ubuntu SMP Wed Sep 28 14:27:32 UTC 2011 x86_64 x86_64    x86_64 GNU/Linux
4

1 回答 1

4

查看node.io源代码,似乎scrape创建了一个Job实例,它在需要时创建一个htmlparser实例,并在解析完成时销毁它(即,当来自请求的所有数据都已输入时)。因此,您无法从单个scrape. 相反,使用 node.io 的低级 API 方法(即new nodeio.Job);看到这个维基页面

于 2011-12-10T17:14:22.947 回答