python / python/cpython

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

Đang mở
#140,970 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

OS-windows stdlib topic-multiprocessing type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với multiprocessing.process.BaseProcess hoặc phần triển khai SpawnProcess trên Windows, sau đó theo dõi cách multiprocessing.popen_spawn_win32.Popen gọi _winapi.CreateProcess. Kiểm tra cách subprocess.Popen xác thực creationflags trên Windows và POSIX, đồng thời xác minh rằng flag được yêu cầu được truyền tới các tiến trình con được tạo trên Windows, trong khi hành vi trên các hệ thống không phải Windows vẫn bị từ chối.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
operating-systems
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.