0

I am trying to use unittest to mock input and print with concurrent.futures.ProcessPoolExecutor, but I get EOFerror everytime it runs. It works perfectly with concurrent.futures.ThreadPoolExecutor, but not with processes. Here is my code:

import concurrent.futures as cf
from unittest import mock


def exercise_5_1(n):
    if n > 0:
        value = int(input("Type the first value: "))
        bigger = smaller = value
        for i in range(2,n+1):
            value = int(input(f"Type the {i}th value: "))
            if bigger <= value:
                bigger = value
            else:
                smaller = value
    else:
        return None
    return (smaller, bigger)

test_5_1 = [((3,),[1,2,3]),((3,),[-1,0,0]),((3,),[7,7,7]),((5,),[-1,2,-3,4,-5])]

def main():
    #with cf.ProcessPoolExecutor() as executor:
    # raises an EOFerror:
    # arg: (3,) resp: [1, 2, 3]
    # exception: ('EOFError', ('EOF when reading a line',))
    # arg: (3,) resp: [-1, 0, 0]
    # exception: ('EOFError', ('EOF when reading a line',))
    # arg: (3,) resp: [7, 7, 7]
    # exception: ('EOFError', ('EOF when reading a line',))
    # arg: (5,) resp: [-1, 2, -3, 4, -5]
    #exception: ('EOFError', ('EOF when reading a line',))
    #Type the first value: Type the first value: Type the first value: Type the first value: 

    # Works perfectly!
    #arg: (3,) resp: [1, 2, 3]
    #res: (1, 3)
    #arg: (3,) resp: [-1, 0, 0]
    #res: (-1, 0)
    #arg: (3,) resp: [7, 7, 7]
    #res: (7, 7)
    #arg: (5,) resp: [-1, 2, -3, 4, -5]
    #res: (-5, 4)
    with cf.ThreadPoolExecutor() as executor:
        for arg,resp in test_5_1:
            print(f'arg: {arg} resp: {resp}')
            with mock.patch('builtins.input', side_effect=resp):
                future  = executor.submit(exercise_5_1,*arg)
                try:
                    print(f'res: {future.result(timeout = None)}')
                except Exception as ex:
                    print(f'exception: {type(ex).__name__, ex.args}')

if __name__ == '__main__':
    main()

What is happening? Is it possible to make it work with processes?

0 Answers0