dart - Improve application performance with isolate -
i have application generate hashed password , generate takes time. think improve performance, let hashed password generator work in seperate core. computer support 3 core processors , think idea use dart:isolate calculate hashed password in other processor core.
i have tried following:
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())); }
as output have got
print1 -> $2a$10$xgggpuiylp2glon2ektw2.kg5qwk4fkiidfa8hkgdpdy1h1aac6lo print2 -> $2a$10$zk..l6hi0nkerbkm2/v6h.5s25qqsjwrszi83.i3czfzlb7pfcw6g isolate -> $2a$10$dm/.25em/3amvgnu2g6wl.sqq2ecgse6duwpc56tvdomgw9zbja36
it seems be, not working concurrency. expected, isolate first place or second place not last. wrong here?
your code working concurrently. when add print() isolate's function this:
void returnhashedpassword(sendport sendport) { print('ok'); receiveport receiveport = new receiveport(); sendport.send('isolate -> ' + new dbcrypt().hashpw('password', new dbcrypt().gensalt())); print('done'); }
the output be:
ok done print1 -> $2a$10$p..gsyxybtjmbrhke.xu1.ifeuihbxxpl9dyclhqx72ykhszb0e1e print2 -> $2a$10$ea.2bnvakh6ubfjiwnwua.jdubhgypsmp2pyopsgd84gcx.spaas. isolate -> $2a$10$.sbmleeuv5u.nasgoe6on.kxq7cnq6yj8ixrbgczgx8tgmcbzt7ny
i guess dart has inbuilt resource allocation algorithm gives stdout processes avoid weird prints.
when change code this:
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'); }
the output is:
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
you can see working concurrent.
regards, robert
Comments
Post a Comment