Search code examples
pythoninputconcurrencyconcurrent.futures

How can I get user input from a function run via concurrent.futures in Python?


So, what I am trying to do is run a function called function, in which I try to get user input and store it in a variable called 'string', which I then return as the function ends. Then, I submit it to a futures.ProcessPoolExecutor, and finally print its result. Here is the code:

#!/usr/bin/python3

from concurrent import futures

def function():
  string = input("Input: ")
  return string

executor = futures.ProcessPoolExecutor()

process = executor.submit(function)

print (process.result())

What goes wrong: Once I run the script, what I get is this:

Input: concurrent.futures.process._RemoteTraceback:
"""
Traceback (most recent call last):
  File "/usr/lib/python3.7/concurrent/futures/process.py", line 232, in _process_worker
    r = call_item.fn(*call_item.args, **call_item.kwargs)
  File "./TestScript.py", line 6, in function
    string = input("Input: ")
EOFError: EOF when reading a line
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./TestScript.py", line 13, in <module>
    print (process.result())
  File "/usr/lib/python3.7/concurrent/futures/_base.py", line 432, in result
    return self.__get_result()
  File "/usr/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
EOFError: EOF when reading a line

I don't ever get to give an input. I have no clue what could be going wrong. Haven't I just fully understood how concurrency and futures work? Thank you in advance!


Solution

  • EOFError is happening because input() is encountering an EOF symbol somehere in your code before receiving any input.

    When I run this in my computer I get a completely different error where concurrent.futures.process has a broken process pool.

    Is this your only code? If there is more you may want to share it to get an appropriate answer.

    EDIT

    
    raise RuntimeError('''
    RuntimeError:
            An attempt has been made to start a new process before the
            current process has finished its bootstrapping phase.
    
            This probably means that you are not using fork to start your
            child processes and you have forgotten to use the proper idiom
            in the main module:
    
                if __name__ == '__main__':
                    freeze_support()
                    ...
    
            The "freeze_support()" line can be omitted if the program
            is not going to be frozen to produce an executable.
    
    concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
    
    

    it looks like your child starts before the parent process has finished.