简体   繁体   中英

Meteor._wrapAsync causes 100% CPU

An app is using meteor-redis package to run a redis query which can sometimes take 30 seconds to return 100k results. During this waiting time, Meteor freezes and takes up 100% CPU while waiting for the query results.

var client = redis.createClient(port, url)
client.zrangebyscoreSync = Meteor._wrapAsync(client.zrangebyscore)

client.zrangebyscoreSync(['game:scores', '', '+inf'], function(err, scores) {

    _.each(scores, function(score, player) {
        var doc = { ... }
        Scores.insert(doc)
    })


})

Question: Is there anyway to let Meteor do something else while waiting for redis to return the huge dataset? If client.zrangebyscore is not wrapped with Meteor._wrapAsync , Meteor would throw the error

 Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.

and the trace points it to the line Scores.insert(doc) .

In general Meteor will allow other methods to run, provided you run this.unblock() on them first, thus spinning those requests into new fibers.

The reason you get 100% CPU usage is probably a combination of the sort of cpu idling fibers does and the task being performed underneath.

If this is an issue, you could try using Meteor.bindEnvironment instead, which won't use future's to wait, and still use callbacks to ensure that everything remains unblocked:

client.zrangebyscore(['game:scores', '', '+inf'], Meteor.bindEnvironment(function(err, result) {
    _.each(scores, function(score, player) {
        var doc = { ... }
        Scores.insert(doc)
    })
}));

Meteor bindEnvironment is almost the same thing as Meteor._wrapAsync, except it's missing the pieces that wait for a result. It looks like you're not using this bit though since you don't look for results coming out of client.zrangebyscoreSync . Meteor.bindEnvironment might be better for this use, it wraps your callbacks in a Fiber, so you can use Meteor code in them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM