有一个spawnUri(uri)功能dart:isolate,但我找不到任何例子.我已经猜到了它的用法,但都失败了.
假设有2个文件,在第一个文件中,它将调用spawnUri第二个文件,并与之通信.
first.dart
import "dart:isolate";
main() {
ReceivePort port = new ReceivePort();
port.receive((msg, _) {
print(msg);
port.close();
});
var c = spawnUri("./second.dart");
c.send(["Freewind", "enjoy dart"], port.toSendPort());
}
Run Code Online (Sandbox Code Playgroud)
second.dart
String hello(String who, String message) {
return "Hello, $who, $message";
}
void isolateMain(ReceivePort port) {
port.receive((msg, reply) => reply.send(hello(msg[0], msg[1]));
}
main() {}
Run Code Online (Sandbox Code Playgroud)
但是这个例子不起作用.我不知道什么是正确的代码,如何修复它?
为我的生活增添一些理智,为instantiate()Dart的mirror图书馆寻找函数作为语法糖: instantiate( class|type|instance, argArray )
class Klass {
int i1;
Klass( int i1 ) {
this.i1 = (i1 is int) ? i1 : 0;
}
}
type ktype = Klass;
Klass kinstance = new Klass( 5 );
Klass test1 = instantiate( Klass, [5] );
Klass test2 = instantiate( ktype, [5] );
Klass test3 = instantiate( kinstance, [5] );
Run Code Online (Sandbox Code Playgroud)
目前90%的我的互动mirrors将被这一功能所覆盖.目前盲目地削减和复制出纯粹的愚蠢.当然比我聪明的人已经做到了!
这是instantiate( type, [constructor, positional, named] )所有场合:
Type实例化类型,也可以是类型的字符串表示形式我有一个生成散列密码的应用程序,并生成它需要时间.我认为为了提高性能,我会让哈希密码生成器在单独的核心中工作.我的计算机支持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()));
}
Run Code Online (Sandbox Code Playgroud)
作为输出我有
Print1 -> $2a$10$XggGPuIyLP2GLon2eKtW2.kG5QwK4fkiIDFa8hkgDPdy1h1AAC6LO
Print2 -> $2a$10$zK..L6Hi0NkeRbkm2/v6H.5s25QQSjwRszI83.i3CzFZlb7pFCW6G
Isolate -> $2a$10$DM/.25em/3amvGNu2G6Wl.SQQ2ECGSE6DUwPc56tvdoMGw9ZBja36
Run Code Online (Sandbox Code Playgroud)
看起来,它不是并发工作.我预计,隔离将是第一个或第二个不是最后一个.我有什么不对吗?
我一直在玩Dart孤立,并且在使用该isolate.pause();功能时遇到问题。
import 'dart:io';
import 'dart:isolate';
main(){
ReceivePort receivePort = new ReceivePort();
Isolate.spawn(isolateEntryPoint, receivePort.sendPort).then((isolate){
isolate.pause(isolate.pauseCapability);
});
}
void isolateEntryPoint(SendPort sendPort){
while(true){
print("isolate is running");
sleep(new Duration(seconds: 2));
}
}
Run Code Online (Sandbox Code Playgroud)
在我的示例中,隔离区基本上只是每2秒打印出一些内容。
从我阅读的文档中,我的理解是上面的代码应该:
但是它不起作用,隔离程序仍在运行,即使我告诉它暂停,它也会每2秒打印一次“隔离程序正在运行”。
我知道您可以通过传递可选参数
来在暂停状态下启动隔离。但最终,我希望能够在任何时候都暂停隔离,而不仅仅是立即行动。paused: trueIsolate.spawn(isolateEntryPoint, receivePort, paused: true)...
我可以找到的关于此功能的唯一文档在官方的dart文档中,因此我可能使用了该isolate.pause()函数不正确。但是,无论哪种方式,演示此功能正确用法的代码示例都将不胜感激。
我有大约 1 万个需要按顺序计算的长时间运行的任务。为此,我决定使用 Isolates。问题是我应该每次为每个单独的任务创建 spawn Isolate 还是我应该只为所有任务的执行创建一个 Isolate。我不知道创建 Isolates 的成本有多高。
创建一个隔离并将其用于所有任务的源:
import 'dart:isolate';
class DataPacket {
SendPort port;
int result;
}
class SquareRootCalculator {
final ReceivePort _masterPort = new ReceivePort();
SendPort _workerPort;
SquareRootCalculator() {
Isolate.spawn(isolateFunction, _masterPort.sendPort).then((isolate) {
_masterPort.listen((data) {
if (_workerPort == null)
_workerPort = data.port;
else {
print(data.toString());
}
});
});
}
input(int n) {
_workerPort.send(n);
}
}
void isolateFunction(SendPort masterPort) {
ReceivePort _workerPort = new ReceivePort();
DataPacket packet = new DataPacket();
packet.port = _workerPort.sendPort;
packet.result = -1;
masterPort.send(packet); …Run Code Online (Sandbox Code Playgroud) 我希望在 Dart 中创建一个 Isolate,我可以以编程方式暂停和恢复。这是我使用的代码。
import 'dart:io';
import 'dart:isolate';
void main() async {
print("Starting isolate");
Isolate isolate;
ReceivePort receivePort = ReceivePort();
isolate = await Isolate.spawn(run, receivePort.sendPort);
print("pausing");
Capability cap = isolate.pause(isolate.pauseCapability);
sleep(Duration(seconds: 5));
print("Resuming");
isolate.resume(cap);
}
void run(SendPort sendPort) {
sleep(Duration(seconds: 2));
print("Woke up, 1");
sleep(Duration(seconds: 2));
print("Woke up, 2");
sleep(Duration(seconds: 2));
print("Woke up, 3");
sleep(Duration(seconds: 2));
print("Woke up, 4");
sleep(Duration(seconds: 2));
print("Woke up, 5");
}
Run Code Online (Sandbox Code Playgroud)
我得到一个像 O/P
Starting isolate
pausing
Woke up, 1
Woke up, 2
Resuming
Woke up, 3 …Run Code Online (Sandbox Code Playgroud) 我对 Dart 很陌生,仍在学习中。据我了解,Dart 在不同的隔离区中执行代码。一个隔离可以启动另一个隔离来执行一些长时间运行的代码。对于每个隔离,都有一个线程和为其分配一些内存。这些隔离体就像一群小虚拟机一样被隔离。
我还从 Dart 文档中了解到 Dart 是一种单线程语言。但是,想一想,每个隔离都有自己的线程。如果isolate A有线程t1,isolate B有线程t2,那么t1和t2就不是同一个线程,对吗?
如果t1和t2是同一个线程,那么t1和t2就不能同时执行代码,这是可笑的。所以,t1和t2必须是不同的线程。
如果是这样,为什么我们说 Dart 是单线程语言呢?
我无法查明为什么我的 dart 程序没有终止。我很确定这与隔离或流控制器有关,并确保它们关闭,但我无法弄清楚问题是什么。
在我所有的StreamController手机上,我都在打电话await streamControllerName.close();,但我认为有一个我不知道的隔离关闭机制。事件循环没有完成并且程序没有退出是否有任何原因?由于代码很长,因此很难提供更多详细信息。
在两个Dart VM之间建立通信的推荐方法是什么?隔离,这里推荐?
如何设置Dart服务器以在即将到来的请求中使用所有可用内核(例如,通过使用多个隔离来提供请求)?