[英]How to make multiple subscription on one ReceivePort dart
I have a task of creating N - isolates, and one main ReceivePort which will be passed to these isolates, and by using this ReceivePort I am passing all of the information which is needed by function to give me a desirable output.我的任务是创建 N 个隔离区,以及一个将传递给这些隔离区的主 ReceivePort,通过使用这个 ReceivePort,我传递了函数所需的所有信息,以便为我提供理想的输出。 It works for the first time, however, any consecutive message to these isolates, and receiving them throws such error
它是第一次工作,但是,向这些隔离区发送的任何连续消息并接收它们都会引发此类错误
This is my code这是我的代码
//main receive port
var controlPort = ReceivePort();
...
class _MainScreenState extends State<MainScreen> {
final ImagePicker _picker = ImagePicker();
Future<ui.Image>? image;
@override
void initState() {
for (int i = 0; i < Platform.numberOfProcessors; i++) {
Future<Isolate> isolate =
Isolate.spawn(Core.readAndMapFast, controlPort.sendPort);
}
}
@override
Widget build(BuildContext context) {
...
static void readAndMapFast(SendPort sendPort) async {
//sending SendPort of a created isolate to the main thread
ReceivePort receivePort = ReceivePort();
sendPort.send(receivePort.sendPort);
receivePort.first.then((message) {
//listening for a message coming from main thread and populating it with all needed
//data
message as Arguments;
Arguments arguments = Arguments(message.imgList, message.lutList,
message.imgHeight, message.imgWidth, message.widthLut, message.order);
//ecexuting all needed operations and after that sending message to the main thread
...
sendPort.send(output);
//in the main thread we are listening for messages from RecievePort we created at
//the very beginning
await for (dynamic message in controlPort.asBroadcastStream()) {
if (message is SendPort) {
//waiting for a message from isolates and if its type is SendPort i.e isolate sending
//its first message we are sending needed arguments to a function
message.send(args[i]);
} else {
message as FinalList;
counter++;
postEditList.add(message);
if (counter == numberOfWorkers) {
Stopwatch timerr = Stopwatch();
timerr.start();
for (int t = 0; t < postEditList.length; t++) {
FinalList list =
postEditList.where((element) => element.order == t).first;
bytesBuilder.add(list.imgSubList);
}
break;
}
}
}
I suggest that I must be using ReceivePort().asBroadcastStream(), however I can not send messages and do other stuff with it.我建议我必须使用 ReceivePort().asBroadcastStream(),但是我不能发送消息并用它做其他事情。 Because after it becomes stream, we do not have SendPort.
因为当它变成流之后,我们就没有SendPort了。
I figured out the solution.我想出了解决办法。 It is pretty easy.
这很容易。 We have one main ReceivePort which we will use to communicate across isolates and main thread, and also we create a BroadcastStream which will be used to listen any updates from our ReceivePort.
我们有一个主要的 ReceivePort,我们将使用它来跨隔离区和主线程进行通信,并且我们还创建了一个 BroadcastStream,用于监听来自我们的 ReceivePort 的任何更新。
var controlPort = ReceivePort();
var broadcastStream = controlPort.asBroadcastStream();
Then all you have to do is to listen updates of broadcastStream not controlPort by然后你所要做的就是监听 broadcastStream 而不是 controlPort 的更新
broadcastStream.listen((event) {});
I hope that it helps to the ones who is stuck on the same problem我希望它对那些被困在同一问题上的人有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.