[Python] multiprocessing.Pool(processes=0) not allowed?

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    In a parallelized script I would like to keep the ability to do the processing in a single CPU. In order to do that it seems that I need to duplicate the code that iterates over the results:

    #!/usr/bin/env python3
    import multiprocessing

    # argparsed parameters:
    num_cpus = 1
    input_filename = 'file.txt'

    def worker(line):
    '''Do something with a line'''
    # dummy processing that requires some CPU time
    for x in range(1000000):
    line.split()
    return line.split()

    def data_iter(filename):
    '''iterate over the data records'''
    with open(filename) as fh:
    for line in fh:
    yield line

    if __name__ == '__main__':
    if (num_cpus >= 2):
    with multiprocessing.Pool( processes = num_cpus - 1 ) as pool:
    for result in pool.imap(worker, data_iter(input_filename)):
    # dummy post-processing (copy#1)
    print(' '.join(result))
    else:
    for line in data_iter(input_filename):
    result = worker(line)
    # dummy post-processing (copy#2)
    print(' '.join(result))


    note: you can use any text file with multiple lines for testing the code.

    Is there a valid reason for multiprocessing.Pool not allowing processes = 0?

    POSTSCRIPT


    I though of one plausible "technical" reason for not allowing it:

    The worker processes in the Pool can't modify the environment of the main program, which wouldn't be true if the worker function is run without spawning a new process.

    Continue reading...

Compartilhe esta Página