Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Python for Unity - Threading

Discussion in 'Formats & External Tools Previews' started by mdooneymill, Jun 21, 2021.

  1. mdooneymill

    mdooneymill

    Joined:
    Feb 7, 2019
    Posts:
    9
    Apologies if this has been covered before but I couldn't find an answer anywhere.

    Does Unity support threading in Python?
    I've got a PySide2 interface which relies on a bunch of web queries and I'm trying to move those off the main thread so that the UI doesn't freeze.

    Using QThread I find that the thread never executes and when using QRunnable the editor crashes. Just wondering if I've missed something obvious somewhere or whether it's just not supported yet.

    Cheers,

    Mark
     
  2. bhudson

    bhudson

    Joined:
    Nov 21, 2014
    Posts:
    31
    Just to be clear, you're on Python 4.0.0-exp.1 right?

    All calls to Unity or to any C# code must be done on the main thread -- even if you just `import` a module implemented in C#, it needs to be imported on the main thread. Otherwise the editor can crash or hang indefinitely.

    Other than that, it's supposed to work to have threads that are entirely in the Python universe doing their thing; indeed the PySide sample has multiple threads.
     
    mdooneymill likes this.