Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawning multiprocessing workers on Windows changes mouse cursor #118293

Closed
tunedal opened this issue Apr 25, 2024 · 0 comments
Closed

Spawning multiprocessing workers on Windows changes mouse cursor #118293

tunedal opened this issue Apr 25, 2024 · 0 comments
Labels
OS-windows topic-multiprocessing type-bug An unexpected behavior, bug, or error

Comments

@tunedal
Copy link
Contributor

tunedal commented Apr 25, 2024

Bug report

Bug description:

The multiprocessing module uses the CreateProcess function on Windows to spawn processes, which has a flag for controlling whether Windows should display feedback to the user in the form of the "Working in Background cursor" (i.e. pointer and hourglass/circle) while the process is launching (see STARTF_FORCEONFEEDBACK in the docs for STARTUPINFO). Since multiprocessing doesn't specify any flags, it gets the default behavior, resulting in this launch feedback mouse cursor being displayed whenever a worker process is spawned.

Since processes in multiprocessing are used as an alternative to threads, not for launching applications but for running background tasks in an existing application, I believe it would make sense to disable the launch feedback. The application is already running and can provide its own UI for displaying the state of background tasks, just as it would when using threads, so the extra feedback is not needed. The launch feedback is more confusing than helpful when launching a background task, because it's not tied to the lifetime of the task – it's just displayed for some arbitrary period of time until it times out waiting for the worker process to display its UI, which it never will.

It's particularly confusing in the case of process pools: The user will see some feedback the first time a task is submitted to the pool (when it starts a worker process) but nothing when subsequent tasks are submitted. And, as mentioned above, it doesn't actually tell the user anything about whether the task is finished, so it's not a useful feature to rely on for this. (Tkinter applications that need a reliable busy cursor can presumably use the new functions added in #72684.)

To fix this, pass the STARTF_FORCEOFFFEEDBACK flag in the STARTUPINFO structure when calling CreateProcess. The relevant Python code is in the multiprocessing.popen_spawn_win32 module, which currently passes None for this parameter.

Screenshot of running a background task with threading.Thread – no mouse cursor change:
taskdemo1

Screenshot of running the same task with multiprocessing.Process – mouse cursor changes when launching the task:
taskdemo2

Steps to reproduce the issue:

  1. Launch the attached taskdemo.txt (which is a Python script, but GitHub doesn't allow attaching .pyw files) using pythonw.exe – the issue is not reproducible from a console application.
  2. Click the "Run Thread task" button. Note any mouse cursor changes. (It will finish after 10 seconds.)
  3. Click the "Run Process task" button. Note any mouse cursor changes.

Expected behavior:
The two buttons should exhibit identical behavior. The mouse cursor should not change in either case.

Actual behavior:
Unlike the "Run Thread task" button, the "Run Process task" button will result in a "Working in Background cursor" being displayed for a few seconds. The cursor will then change back before the task is actually completed (which happens after 10 seconds, as indicated by the app's UI).

The attached demo script lets you monkeypatch the multiprocessing module at runtime by checking the "Enable patch" checkbox. The steps above will then produce the expected behavior.

CPython versions tested on:

3.10, 3.11

Operating systems tested on:

Windows

Linked PRs

@tunedal tunedal added the type-bug An unexpected behavior, bug, or error label Apr 25, 2024
tunedal added a commit to tunedal/cpython that referenced this issue Apr 25, 2024
tunedal added a commit to tunedal/cpython that referenced this issue Apr 26, 2024
tunedal added a commit to tunedal/cpython that referenced this issue Apr 26, 2024
tunedal added a commit to tunedal/cpython that referenced this issue Apr 26, 2024
tunedal added a commit to tunedal/cpython that referenced this issue Apr 26, 2024
tunedal added a commit to tunedal/cpython that referenced this issue Apr 26, 2024
tunedal added a commit to tunedal/cpython that referenced this issue Apr 27, 2024
tunedal added a commit to tunedal/cpython that referenced this issue Apr 27, 2024
tunedal added a commit to tunedal/cpython that referenced this issue Apr 27, 2024
zooba pushed a commit that referenced this issue Apr 28, 2024
@zooba zooba closed this as completed Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows topic-multiprocessing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants