Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Async await/Coroutines aren't good for my IO case

Discussion in 'Scripting' started by CalipsDZ, Jan 10, 2020.

  1. CalipsDZ

    CalipsDZ

    Joined:
    Feb 5, 2019
    Posts:
    6
    So I'm making an application with Unity where a
    SerialPort 
    is connected to the PC and through which I need 60Hz updates (or close) information(around 200bytes or less) and another IO device(camera). I used
    Coroutines 
    and also
    Async 
    await to deal with this input but when I run the IO in Unity (with keyboard) I get a huge FPS drop.
    Now if I use
    SerialPort.BaseStream.ReadAsync
    there isn't an FPS drop, but the information will be slowly received.
    My question would be : Did anyone get into this kind of issue ? Any suggestions ? also do you think I should turn to Threads ?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Coroutines run on the main thread. An async method runs on the main thread until it calls await. What you might want is to run this entirely in a separate thread instead of on the main thread.
     
  3. CalipsDZ

    CalipsDZ

    Joined:
    Feb 5, 2019
    Posts:
    6
    Thanks for the answer @Joe-Censored ! Do you mean run all the content of the async method in a background thread ?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah, I'd be considering starting a new thread, do all serial port work in that new thread, and sync it with the main thread with something like a ConcurrentQueue. That's what I do for my networking implementation, and I'd think the same approach may be appropriate for this kind of thing.
     
    CalipsDZ likes this.