1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python Multiprocessing - losing command prompt window

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    I am running the following Python program to start a few processes to run simultaneously. Each process will run for several seconds. This is being started from a command prompt. But if I interrupt the execution (ctl-C) I often get a console message 'keyboard interrupt' along with a long traceback and the command prompt is forever lost. I must kill the command prompt and start a new one. Depending upon timing, sometimes it will allow new commands to be enterred, but there is no input prompt C\xxx\xxx>.

    I expected the ctl-C just to terminate the mainline being executed which would in turn kill the child processes that were running. But this was not the case.

    Why can't this program be interrupted without bringing down the Command Prompt from running again?


    from multiprocessing import Process, Pool
    import os
    import time

    def proc(name):
    print('\nStarting process name:',name, 'PID: ', os.getpid())
    for i in range(1,5):
    print (os.getpid(),': ',i,'\n')
    time.sleep(1)

    if __name__ == '__main__':
    print ('\nMain process ID:', os.getpid(), '\n')
    numServers = 3
    serverNum = []
    for i in range(numServers):
    serverNum.append(i+1)
    print ('serverNum: ', serverNum)
    with Pool(numServers) as p:
    p.map(proc, serverNum)

    # --> hang here until the processes complete
    # Execution stops when all processes complete
    print ('Ending mainline task', os.getpid())
    exit(0)


    The program output is:


    Main process ID: 40536

    serverNum: [1, 2, 3]

    Starting process name: 1 PID: 34508
    34508 : 1


    Starting process name: 2 PID: 2200
    2200 : 1


    Starting process name: 3 PID: 43476
    43476 : 1

    34508 : 2

    2200 : 2

    43476 : 2

    34508 : 3

    2200 : 3

    43476 : 3

    34508 : 4

    2200 : 4

    43476 : 4

    Ending mainline task 40536

    Continue reading...

Compartilhe esta Página