0

I am trying to get an input from a user within a specific time-limit. If input is not given, then print "Time Up!". Otherwise I need to print the input back.

Code: To make input live only for some time

from multiprocessing import Process,Queue
from time import sleep

def getInput(q):
    ip=input("Enter Some Data Within 5 Seconds :")
    q.put(ip)

if __name__=="__main__":
    queue=Queue()
    p1=Process(target=getInput,args=(queue))
    p1.start()
    sleep(5)
    if not queue.empty():
        ip=queue.get()
        print(f"Entered Input : {ip}")
    else:
        print("Time Up!")
        p1.terminate()
        p1.join()
        p1.close()

Output:

Enter Some Data Within 5 Seconds :Process Process-1:
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1008.0_x64__qbz5n2kfra8p0\Lib\multiprocessing\process.py", line 314, in _bootstrap
    self.run()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1008.0_x64__qbz5n2kfra8p0\Lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "d:\Pesonal\Learning\Codes\Socket\Project-1\Temp\untitle.py", line 7, in getInput
    ip=input("Enter Some Data Within 5 Seconds :")
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
EOFError: EOF when reading a line
Time Up!

0 Answers0