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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    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.