pybind / pybind/pybind11

[QUESTION] Why does `py::eval_file` not block and not return in QT?

Open
#2,653 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

I connect a button to bellowing function call:

void EvalFile(const std::string& py_file) {
    std::lock_guard<std::mutex> guard(g_python_mutex_);
    py::eval_file(py_file, scope);//  here will not block qt main thread, but also not return
}

First, click the button once, then the main thread will call this function but not step out of it. If paused, it always stops at

PyObject *result = PyRun_FileEx(f, fname_str.c_str(), start, global.ptr(), local.ptr(), closeFile);

which is a sub block of py::eval_file. The python file evaluated will open a UI, and I will not close it.

Then, click the button once again. The main thread could also step into this function.

Why does not this function block?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the py::eval_file entry point and the PyRun_FileEx call shown in the report, then reproduce the behavior from a Qt button callback with a Python file that opens a UI. Trace whether control returns only after that UI closes and document the observed interaction between the Qt main thread and Python execution.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
backend, desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.