【问题标题】:imagemagick in meteorjs (with the help of meteor-router and fibers)流星js中的imagemagick(借助流星路由器和光纤)
【发布时间】:2012-12-01 05:25:24
【问题描述】:

我无法在meteorjs 中使用imagemagick。我正在开发一个小型 svg->png 转换器,其中包含一个用于提供转换后的图像的 rest api。我用meteor-router实现了其余的api。 imagemagick 转换有效。但是,我无法将转换结果写入 http 响应。我试图通过使用光纤来消除异步性来解决这个问题。但这仍然行不通。基本上,所有 request.write 调用在 yield 执行后都会被忽略。这是我的代码:

Meteor.Router.add({
  '/image/:hash' : function(hash) {

    var svg = Images.findOne({'hash' : hash}).svg;

    var request = this.request;
    var response = this.response;

    Fiber(function() {
      var fiber = Fiber.current;

      response.writeHead(200, {'Content-Type':'image/png'});

      var convert = imagemagick.convert(['svg:-', 'png:-']);

      convert.on('data', function(data) {
        response.write("doesn't work");
        //response.write(data);
      });

      convert.on('end', function() {
        response.write("doesn't work");
        //response.end();
        fiber.run({});
      });

      convert.stdin.write(svg);
      convert.stdin.end();

      response.write("works");
      Fiber.yield();
      response.write("doesn't work");
    }).run();

  }
});

我对meteorjs 很陌生。因此,我可能会完全错误地使用 Fiber。或者我根本不应该使用纤维。有人可以帮忙吗?

【问题讨论】:

  • 如果convert.on('data', ...) 回调从未触发,我认为您的问题与光纤无关。您确定此代码在香草节点中有效吗?你在使用 node-imagemagick (github.com/rsms/node-imagemagick) 吗?我在他们的文档中找不到对.on 的引用——他们建议将第二个回调参数传递给.convert

标签: imagemagick meteor node-fibers


【解决方案1】:

感谢meteor-router的作者,我能够解决这个问题。我以错误的方式使用光纤。如https://github.com/laverdet/node-fibers#futures 所述,不建议在您的代码和原始 API 之间没有抽象的情况下使用 Fiber。

幸运的是,光纤提供了一种称为未来的抽象,可以用于我的用例!这是工作代码:

var require = __meteor_bootstrap__.require,
Future  = require('fibers/future');

Meteor.startup(function() {
  Meteor.Router.add('/image/:hash', function(hash) {
    var response = this.response;
    var fut = new Future();

    response.writeHead(200, {'Content-Type':'text/plain'});

    setTimeout(function(){ 
      response.write("hello hello");
      fut.ret();
    }, 1); 

    fut.wait();
  });
});

【讨论】:

    【解决方案2】:

    我做了更多调查。这个问题与 imagemagick 是正交的。例如:以下代码 sn-ps 在流星路由器中也不起作用:

    示例 1:

    Meteor.startup(function() {                                                        
      Meteor.Router.add({                                                              
        '/image/:hash' : function(hash) 
    
          var request = this.request;                                                  
          var response = this.response;                                                
    
          response.write("outside");                                                   
    
          setTimeout(function(){                                                       
            response.write("inside");                                                  
            response.end();                                                            
          }, 1); 
        }                                                                           
      }); 
    

    示例 2:

    Meteor.startup(function() {                                                        
      Meteor.Router.add({                                                              
        '/image/:hash' : function(hash) 
          var request = this.request;                                               
          var response = this.response;                                             
    
          response.write("outside");                                                
    
          Fiber(function() {                                                        
            var fiber = Fiber.current;                                              
    
            setTimeout(function(){                                                  
              response.write("inside");                                             
              response.end();                                                       
            }, 1);                                                                  
            Fiber.yield();                                                          
          }).run();   
        }                                                                           
      }); 
    

    我认为这是流星路由器的一般问题。因为这两个示例都适用于纯 nodejs。

    【讨论】:

      猜你喜欢
      • 2013-12-05
      • 2013-10-04
      • 2014-07-30
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多