python / python/cpython

Allow passing creationflags to `multiprocessing` Process implementations to be used to set process creation flags on Windows

Abierto
#140,970 1 comentario 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

OS-windows stdlib topic-multiprocessing type-feature
Lenguaje dominante
Python
Estrellas
77.2k
Forks
36k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Feature or enhancement

Proposal:

On Windows, there are various process creation flags that control the behavior of spawned processes that can control important behavior such as whether they can receive interrupt signals (and who can send them interrupt signals). A common flag to pass to child process on Windows is CREATE_NEW_PROCESS_GROUP as that allows the child process to receive signals (Ctrl+Break) from the parent process without requiring those signals to be sent to all processes in the console group. Without CREATE_NEW_PROCESS_GROUP, the only way to send an interrupt to a child process on Windows is to send Ctrl+C to all processes in the current console group (creating a new console group is another creationflags option) which would potentially include the current process and one or more parents that may not gracefully handle the interrupt.

The low level subprocess.Popen API includes an optional creationflags argument that is passed to the internal _winapi.CreateProcess call that spawns the actual child process (a ValueError is raised if creationflags is set on a posix OS). I'd like to propose implementing the same behavior in multiprocessing.process.BaseProcess (or in the win32 implementation of multiprocessing.context.SpawnProcess). Accept an optional creationflags member that can be set with a Windows creation flag when running on win32 and apply that value in the call to _winapi.CreateProcess in multiprocessing.popen_spawn_win32.Popen.

This would be a relatively minor API change, but would give much greater control over sub-process behavior on Windows when using the multiprocessing APIs, including better ability to signal spawned processes.

class BaseProcess(object):
    '''
    Process objects represent activity that is run in a separate process

    The class is analogous to `threading.Thread`
    '''
    def _Popen(self):
        raise NotImplementedError

    def __init__(self, group=None, target=None, name=None, args=(), kwargs=None,
                 *, daemon=None, creationflags=0):
        # omitting existing code
        self.creationflags = creationflags

    @property
    def creationflags(self):
        return self._creationflags

    @creationflags.setter
    def creationflags(self, creationflags):
        # Match the behavior of subprocess.Popen creationflags
        assert isinstance(creationflags, int), 'creationflags must be an integer'
        assert creationflags == 0 or sys.platform == 'win32', "creationflags is only supported on Windows platforms"
        self._creationflags = creationflags
class Popen(object):
    '''
    Start a subprocess to run the code of a process object
    '''
    method = 'spawn'

    def __init__(self, process_obj):
        # omitting existing code
        hp, ht, pid, tid = _winapi.CreateProcess(
                    python_exe, cmd,
                    None, None, False, process_obj.creationflags, env, None,
                    STARTUPINFO(dwFlags=STARTF_FORCEOFFFEEDBACK))
Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comience con multiprocessing.process.BaseProcess o con la implementación de Windows SpawnProcess y, a continuación, siga cómo multiprocessing.popen_spawn_win32.Popen llama a _winapi.CreateProcess. Compruebe cómo subprocess.Popen valida creationflags en Windows y POSIX, y verifique que la marca solicitada llegue a los procesos hijo creados en Windows, mientras que el comportamiento en sistemas no Windows siga siendo rechazado.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
operating-systems
Tipo de issue
Nueva funcionalidad
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.