Skip to content

Commit b5d9e08

Browse files
JamesGKentvstinner
authored andcommitted
bpo-31884 subprocess: add Windows constants for process priority (#4150)
1 parent 54cc0c0 commit b5d9e08

File tree

4 files changed

+119
-6
lines changed

4 files changed

+119
-6
lines changed

Doc/library/subprocess.rst

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,20 @@ functions.
516516

517517
If given, *startupinfo* will be a :class:`STARTUPINFO` object, which is
518518
passed to the underlying ``CreateProcess`` function.
519-
*creationflags*, if given, can be :data:`CREATE_NEW_CONSOLE` or
520-
:data:`CREATE_NEW_PROCESS_GROUP`. (Windows only)
519+
*creationflags*, if given, can be one or more of the following flags:
520+
521+
* :data:`CREATE_NEW_CONSOLE`
522+
* :data:`CREATE_NEW_PROCESS_GROUP`
523+
* :data:`ABOVE_NORMAL_PRIORITY_CLASS`
524+
* :data:`BELOW_NORMAL_PRIORITY_CLASS`
525+
* :data:`HIGH_PRIORITY_CLASS`
526+
* :data:`IDLE_PRIORITY_CLASS`
527+
* :data:`NORMAL_PRIORITY_CLASS`
528+
* :data:`REALTIME_PRIORITY_CLASS`
529+
* :data:`CREATE_NO_WINDOW`
530+
* :data:`DETACHED_PROCESS`
531+
* :data:`CREATE_DEFAULT_ERROR_MODE`
532+
* :data:`CREATE_BREAKAWAY_FROM_JOB`
521533

522534
Popen objects are supported as context managers via the :keyword:`with` statement:
523535
on exit, standard file descriptors are closed, and the process is waited for.
@@ -803,8 +815,8 @@ on Windows.
803815
:class:`Popen` is called with ``shell=True``.
804816

805817

806-
Constants
807-
^^^^^^^^^
818+
Windows Constants
819+
^^^^^^^^^^^^^^^^^
808820

809821
The :mod:`subprocess` module exposes the following constants.
810822

@@ -851,6 +863,84 @@ The :mod:`subprocess` module exposes the following constants.
851863

852864
This flag is ignored if :data:`CREATE_NEW_CONSOLE` is specified.
853865

866+
.. data:: ABOVE_NORMAL_PRIORITY_CLASS
867+
868+
A :class:`Popen` ``creationflags`` parameter to specify that a new process
869+
will have an above average priority.
870+
871+
.. versionadded:: 3.7
872+
873+
.. data:: BELOW_NORMAL_PRIORITY_CLASS
874+
875+
A :class:`Popen` ``creationflags`` parameter to specify that a new process
876+
will have a below average priority.
877+
878+
.. versionadded:: 3.7
879+
880+
.. data:: HIGH_PRIORITY_CLASS
881+
882+
A :class:`Popen` ``creationflags`` parameter to specify that a new process
883+
will have a high priority.
884+
885+
.. versionadded:: 3.7
886+
887+
.. data:: IDLE_PRIORITY_CLASS
888+
889+
A :class:`Popen` ``creationflags`` parameter to specify that a new process
890+
will have an idle (lowest) priority.
891+
892+
.. versionadded:: 3.7
893+
894+
.. data:: NORMAL_PRIORITY_CLASS
895+
896+
A :class:`Popen` ``creationflags`` parameter to specify that a new process
897+
will have an normal priority. (default)
898+
899+
.. versionadded:: 3.7
900+
901+
.. data:: REALTIME_PRIORITY_CLASS
902+
903+
A :class:`Popen` ``creationflags`` parameter to specify that a new process
904+
will have realtime priority.
905+
You should almost never use REALTIME_PRIORITY_CLASS, because this interrupts
906+
system threads that manage mouse input, keyboard input, and background disk
907+
flushing. This class can be appropriate for applications that "talk" directly
908+
to hardware or that perform brief tasks that should have limited interruptions.
909+
910+
.. versionadded:: 3.7
911+
912+
.. data:: CREATE_NO_WINDOW
913+
914+
A :class:`Popen` ``creationflags`` parameter to specify that a new process
915+
will not create a window
916+
917+
.. versionadded:: 3.7
918+
919+
.. data:: DETACHED_PROCESS
920+
921+
A :class:`Popen` ``creationflags`` parameter to specify that a new process
922+
will not inherit its parent's console.
923+
This value cannot be used with CREATE_NEW_CONSOLE.
924+
925+
.. versionadded:: 3.7
926+
927+
.. data:: CREATE_DEFAULT_ERROR_MODE
928+
929+
A :class:`Popen` ``creationflags`` parameter to specify that a new process
930+
does not inherit the error mode of the calling process. Instead, the new
931+
process gets the default error mode.
932+
This feature is particularly useful for multithreaded shell applications
933+
that run with hard errors disabled.
934+
935+
.. versionadded:: 3.7
936+
937+
.. data:: CREATE_BREAKAWAY_FROM_JOB
938+
939+
A :class:`Popen` ``creationflags`` parameter to specify that a new process
940+
is not associated with the job.
941+
942+
.. versionadded:: 3.7
943+
854944
.. _call-function-trio:
855945

856946
Older high-level API

Lib/subprocess.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,23 @@ def __init__(self, *, dwFlags=0, hStdInput=None, hStdOutput=None,
164164
from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP,
165165
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
166166
STD_ERROR_HANDLE, SW_HIDE,
167-
STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW)
167+
STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW,
168+
ABOVE_NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS,
169+
HIGH_PRIORITY_CLASS, IDLE_PRIORITY_CLASS,
170+
NORMAL_PRIORITY_CLASS, REALTIME_PRIORITY_CLASS,
171+
CREATE_NO_WINDOW, DETACHED_PROCESS,
172+
CREATE_DEFAULT_ERROR_MODE, CREATE_BREAKAWAY_FROM_JOB)
168173

169174
__all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP",
170175
"STD_INPUT_HANDLE", "STD_OUTPUT_HANDLE",
171176
"STD_ERROR_HANDLE", "SW_HIDE",
172177
"STARTF_USESTDHANDLES", "STARTF_USESHOWWINDOW",
173-
"STARTUPINFO"])
178+
"STARTUPINFO",
179+
"ABOVE_NORMAL_PRIORITY_CLASS", "BELOW_NORMAL_PRIORITY_CLASS",
180+
"HIGH_PRIORITY_CLASS", "IDLE_PRIORITY_CLASS",
181+
"NORMAL_PRIORITY_CLASS", "REALTIME_PRIORITY_CLASS",
182+
"CREATE_NO_WINDOW", "DETACHED_PROCESS",
183+
"CREATE_DEFAULT_ERROR_MODE", "CREATE_BREAKAWAY_FROM_JOB"])
174184

175185
class Handle(int):
176186
closed = False
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
added required constants to subprocess module for setting priotity on windows

Modules/_winapi.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,18 @@ PyInit__winapi(void)
15951595
WINAPI_CONSTANT(F_DWORD, WAIT_OBJECT_0);
15961596
WINAPI_CONSTANT(F_DWORD, WAIT_ABANDONED_0);
15971597
WINAPI_CONSTANT(F_DWORD, WAIT_TIMEOUT);
1598+
1599+
WINAPI_CONSTANT(F_DWORD, ABOVE_NORMAL_PRIORITY_CLASS);
1600+
WINAPI_CONSTANT(F_DWORD, BELOW_NORMAL_PRIORITY_CLASS);
1601+
WINAPI_CONSTANT(F_DWORD, HIGH_PRIORITY_CLASS);
1602+
WINAPI_CONSTANT(F_DWORD, IDLE_PRIORITY_CLASS);
1603+
WINAPI_CONSTANT(F_DWORD, NORMAL_PRIORITY_CLASS);
1604+
WINAPI_CONSTANT(F_DWORD, REALTIME_PRIORITY_CLASS);
1605+
1606+
WINAPI_CONSTANT(F_DWORD, CREATE_NO_WINDOW);
1607+
WINAPI_CONSTANT(F_DWORD, DETACHED_PROCESS);
1608+
WINAPI_CONSTANT(F_DWORD, CREATE_DEFAULT_ERROR_MODE);
1609+
WINAPI_CONSTANT(F_DWORD, CREATE_BREAKAWAY_FROM_JOB);
15981610

15991611
WINAPI_CONSTANT("i", NULL);
16001612

0 commit comments

Comments
 (0)