简体   繁体   中英

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. 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. Because after it becomes stream, we do not have 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.

var controlPort = ReceivePort();
var broadcastStream = controlPort.asBroadcastStream();

Then all you have to do is to listen updates of broadcastStream not controlPort by

broadcastStream.listen((event) {});

I hope that it helps to the ones who is stuck on the same problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM