【问题标题】:Improve application performance with isolate使用隔离提高应用程序性能
【发布时间】:2014-06-27 06:44:45
【问题描述】:

我有一个生成散列密码的应用程序,生成它需要时间。我认为为了提高性能,我会让散列密码生成器在单独的核心中工作。我的计算机支持 3 个核心处理器,我认为使用 dart:isolate 计算其他处理器核心中的哈希密码是个好主意。

我尝试了以下方法:

import 'dart:isolate';
import 'package:dbcrypt/dbcrypt.dart';

main() {

  ReceivePort receivePort = new ReceivePort();
  var receivePortPw = new ReceivePort();

  receivePortPw.listen((msg) {
     print(msg); 
  });

  Isolate.spawn(ReturnHashedPassword, receivePortPw.sendPort);
  print('Print1 -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));  
  print('Print2 -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));  

}

void ReturnHashedPassword(SendPort sendPort)
{
    ReceivePort receivePort = new ReceivePort();
    sendPort.send('Isolate -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));    
}

作为我得到的输出

Print1 -> $2a$10$XggGPuIyLP2GLon2eKtW2.kG5QwK4fkiIDFa8hkgDPdy1h1AAC6LO
Print2 -> $2a$10$zK..L6Hi0NkeRbkm2/v6H.5s25QQSjwRszI83.i3CzFZlb7pFCW6G
Isolate -> $2a$10$DM/.25em/3amvGNu2G6Wl.SQQ2ECGSE6DUwPc56tvdoMGw9ZBja36

似乎是,它不是并发工作。我预计,隔离将是第一名或第二名,而不是最后一名。我在这里做错了什么?

【问题讨论】:

    标签: dart dart-isolates


    【解决方案1】:

    您的代码正在同时工作。当您像这样将 print() 添加到 Isolate 的函数时:

    void ReturnHashedPassword(SendPort sendPort)
    {
        print('ok');
        ReceivePort receivePort = new ReceivePort();
        sendPort.send('Isolate -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
        print('done');
    }
    

    输出将是:

    ok
    done
    Print1 -> $2a$10$p..GSYxybtjmbrhKe.xu1.IfEUihBxXPL9DYCLHqx72yKHsZB0e1e
    Print2 -> $2a$10$eA.2bNvakH6uBFjiWNwua.jDUBhgYPsMP2PyOpsGd84GCx.spaAS.
    Isolate -> $2a$10$.sBmleeuV5U.NaSGOE6ON.kxQ7Cnq6yj8IXRBgCZgx8TGmcBZT7Ny
    

    我猜 dart 有一些内置的资源分配算法,可以为进程提供标准输出以避免奇怪的打印。

    当您像这样更改代码时:

    import 'dart:isolate';
    import 'package:dbcrypt/dbcrypt.dart';
    import 'dart:async';
    
    main() {
    
      //ReceivePort receivePort = new ReceivePort();
      var receivePortPw = new ReceivePort();
      receivePortPw.listen((msg) {
         print(msg); 
      });
    
    
      Future<Isolate> f = Isolate.spawn(ReturnHashedPassword, receivePortPw.sendPort);
      f.then((Isolate i) {
        print('Print1 -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
        print('Print2 -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
      });
    }
    
    void ReturnHashedPassword(SendPort sendPort)
    {
        print('ok');
        ReceivePort receivePort = new ReceivePort();
        sendPort.send('Isolate -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
        print('done');
    }
    

    输出是:

    ok
    Print1 -> $2a$10$zABOnUhKUn.GqERW2Euu7.HpzNizwTDyDbSSLe0b1XL6o9jEo/4dm
    done
    Print2 -> $2a$10$SE.eczx2i1o2dfey.NpSI.gZXhJU9KDWPAp1UtOFBjUI/ltjppwy2
    Isolate -> $2a$10$s.B.0dnGQ0KO1za..I5uL.U1ARKLUK/Jtv/.O8BjP7gQroidvesEC
    

    您可以看到它正在并发工作。

    问候,罗伯特

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 2010-11-12
      • 1970-01-01
      • 2023-04-07
      • 2016-05-03
      相关资源
      最近更新 更多