-1

may i ask how to multithreading some functions in dart for example i have 16 Future functions i want first to execute this function in the first thread

await GetRoot(); //Future<void>

then after the first thread end i want to execute theses functions in the second thread -togather at the same time-

 await GetLocalRadio();//Future<void>
  await GetMusic();//Future<void>
  await GetTalk();//Future<void>
  await GetSports();//Future<void>
  await GetByLocation();//Future<void>
  await GetByLanguage();//Future<void>
  await GetPodcasts();//Future<void>

then after the second thread is finished , i want to execute these threads togather at the same time in the next thread

 await GetPodcasts2();//Future<void>
  await GetByLocation2();//Future<void>

then when the second thread end i want when the third functions is finished to execute these functions togather at the same time in a new thread

  await FillPdcasts();//Future<void>
  await FillLocalRadio();//Future<void>
  await FillMusic();//Future<void>
  await FillTalk();//Future<void>
  await FillSports();//Future<void>
  await FillByLocation();//Future<void>

here is a simple of the content of all methods that i have

 Future<void> FillByLocation()
  async {
    for (var entry in xByLocation2) {
        final responseFinalByLocation2 = await http.Client().get(entry.split('^')[1], headers: {'Content-Type': 'application/json; charset=utf-8'});
        try{
          final FinalByLocation2final = allInfosFromJson(utf8.decode(responseFinalByLocation2.bodyBytes));
          for (int i = 0; i < FinalByLocation2final.body.length; i++) {
            for (int j = 0; j < FinalByLocation2final.body[i].children.length; j++) {
              if(FinalByLocation2final.body[i].children[j].text != null && FinalByLocation2final.body[i].children[j].image != null)
              {
                if(!xListFinalAllInfos.contains(FinalByLocation2final.body[i].children[j].text + "^" + FinalByLocation2final.body[i].children[j].subtext + "^" + FinalByLocation2final.body[i].children[j].nowPlayingId + "^" + FinalByLocation2final.body[i].children[j].currentTrack+ "^" + FinalByLocation2final.body[i].children[j].url+ "^" + FinalByLocation2final.body[i].children[j].image))
                  {
                    setState(() {
                      xListFinalAllInfos.add(FinalByLocation2final.body[i].children[j].text + "^" + FinalByLocation2final.body[i].children[j].subtext + "^" + FinalByLocation2final.body[i].children[j].nowPlayingId + "^" + FinalByLocation2final.body[i].children[j].currentTrack+ "^" + FinalByLocation2final.body[i].children[j].url+ "^" + FinalByLocation2final.body[i].children[j].image);
                      // ListFinalAllInfos.putIfAbsent(
                      //     FinalByLocation2final.body[i].children[j].text, () =>
                      // FinalByLocation2final.body[i].children[j].image );
                    });
                  }
              }
            }
          }
        }catch(e){}
      }
  }

Because when I execute all the functions and await all of them, then this will take about 30 seconds. This is a lot of time, so I need to make the time less than 30 seconds.

10
  • It's impossible for us to tell if these functions can even be run in isolates without showing us the contents of these functions. Are you already running these functions in isolates? Oct 23, 2020 at 12:02
  • thanks for your help. no it's not impossible cause i mentioned that these methods are Future<void> so i think it's not possible to run in isolates cause their types are Future of void, isn't ?
    – test firma
    Oct 23, 2020 at 12:05
  • Are you saying these functions cannot be run in isolates? You cannot get close to multithreading without isolates. Oct 23, 2020 at 12:06
  • sorry i just saw your comment and repaid, i will update the thread and write the content of one of the methods for example cause they are all the same
    – test firma
    Oct 23, 2020 at 12:07
  • It's definitely not possible to run this method in an isolate in its current state. What's the motivation to do multithreading? What's wrong with just asynchrony? Oct 23, 2020 at 12:09

1 Answer 1

0

Multithreading is not an option here. Just stick with asynchrony.

All of your awaits are making your code much slower. You're forcing the previous function to finish before the next function can even start. Call them all at the same time and use Future.wait.

await GetRoot();

List<Future<void>> toWait = [];
toWait.add(GetLocalRadio());
toWait.add(GetMusic());
...

await Future.wait(toWait);
toWait.clear();

toWait.add(GetPodcasts2());
toWait.add(GetByLocation2());

await Future.wait(toWait);
toWait.clear();

toWait.add(FillPdcasts());
toWait.add(FillLocalRadio());
...

await Future.wait(toWait);
toWait.clear();

Additionally, the work you're doing within these function is also implemented inefficiently. You're waiting for each http call to finish before processing the data from the call and starting the next one. This can be made more efficient.

The following code uses .then to register callbacks for each of the network calls, stores the futures to a list, and uses Future.wait at the very end to ensure they're all completed before the wrapper function returns.

 Future<void> FillByLocation()
  async {
    List<Future<void>> toWait = [];
    for (var entry in xByLocation2) {
        toWait.add(http.Client().get(entry.split('^')[1], headers: {'Content-Type': 'application/json; charset=utf-8'}).then((responseFinalByLocation2){
        try{
          final FinalByLocation2final = allInfosFromJson(utf8.decode(responseFinalByLocation2.bodyBytes));
          for (int i = 0; i < FinalByLocation2final.body.length; i++) {
            for (int j = 0; j < FinalByLocation2final.body[i].children.length; j++) {
              if(FinalByLocation2final.body[i].children[j].text != null && FinalByLocation2final.body[i].children[j].image != null)
              {
                if(!xListFinalAllInfos.contains(FinalByLocation2final.body[i].children[j].text + "^" + FinalByLocation2final.body[i].children[j].subtext + "^" + FinalByLocation2final.body[i].children[j].nowPlayingId + "^" + FinalByLocation2final.body[i].children[j].currentTrack+ "^" + FinalByLocation2final.body[i].children[j].url+ "^" + FinalByLocation2final.body[i].children[j].image))
                  {
                    setState(() {
                      xListFinalAllInfos.add(FinalByLocation2final.body[i].children[j].text + "^" + FinalByLocation2final.body[i].children[j].subtext + "^" + FinalByLocation2final.body[i].children[j].nowPlayingId + "^" + FinalByLocation2final.body[i].children[j].currentTrack+ "^" + FinalByLocation2final.body[i].children[j].url+ "^" + FinalByLocation2final.body[i].children[j].image);
                      // ListFinalAllInfos.putIfAbsent(
                      //     FinalByLocation2final.body[i].children[j].text, () =>
                      // FinalByLocation2final.body[i].children[j].image );
                    });
                  }
              }
            }
          }
        }catch(e){}
        }));
      }
    await Future.wait(toWait);
  }
4
  • how can i made that more efficient ?
    – test firma
    Oct 23, 2020 at 12:31
  • thanks really for your help, i just saw your new edited comment., you teach me a lot today . Many thanks.
    – test firma
    Oct 23, 2020 at 13:16
  • why Future.wait(toWait); shows an error , what should the type of toWait, cause the syntax said that the type of toWait must not be a List , so i changed it to var , and the same syntax issue appares.
    – test firma
    Oct 23, 2020 at 13:24
  • @testfirma Try adding <Future<void>> to the end of the list declaration. If this does not work, provide the full error. Oct 23, 2020 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.