Skip to content

Is it possible call evalScript in multithread #217

Closed

Description

@CreativeLau

I set unique named module by "createUniqueModule" function for each thread. When using these modules' evalScript funciton in multithread. I got access confit error in python.dll.

Since PythonQt is a singleton. I think there may have multithread accesing confilt somewhere in PythonQt even I have unique named module.

Activity

  1. usiems commented on May 23, 2024

    @usiems
    Contributor

    Let's see if I can get the necessary parts together:

    You need to call PythonQt::setEnableThreadSupport(true); during the initialization of PythonQt. And then release the global Python interpreter lock (GIL) with myPythonThreadState = PyEval_SaveThread(); until just before PythonQt is finalized, when you acquire it again with PyEval_RestoreThread(myPythonThreadState).

    Inbetween you should then use the PYTHONQT_GIL_SCOPE define to prefix code like createUniqueModule or evalScript. This makes sure the GIL is acquired before Python code is executed.

    Note though that this means these code sections can not run in parallel (or at least only if some Python function does release the GIL, usually because of - potentially longer running - IO operations). I believe that this restriction will finally be removed in one of the next Python versions.

  2. locked and limited conversation to collaborators on May 23, 2024
  3. converted this issue into a discussion #218 on May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions