[Python] Sending ^C to Python subprocess objects on Windows

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have a test harness (written in Python) that needs to shut down the program under test (written in C) by sending it ^C. On Unix,

    proc.send_signal(signal.SIGINT)


    works perfectly. On Windows, that throws an error ("signal 2 is not supported" or something like that). I am using Python 2.7 for Windows, so I have the impression that I should be able to do instead

    proc.send_signal(signal.CTRL_C_EVENT)


    but this doesn't do anything at all. What do I have to do? This is the code that creates the subprocess:

    # Windows needs an extra argument passed to subprocess.Popen,
    # but the constant isn't defined on Unix.
    try: kwargs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP
    except AttributeError: pass
    proc = subprocess.Popen(argv,
    stdin=open(os.path.devnull, "r"),
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    **kwargs)

    Continue reading...

Compartilhe esta Página