0

I want to write a python script in which if I doesn't enter any value through input command, then it should assign a default a value to my variable after 30 seconds, my platform is windows 7

from func_timeout import func_timeout, FunctionTimedOut

def doit():

    value = input("enter")

try:
    doitReturnValue = func_timeout(5, doit)

except FunctionTimedOut:
    value = "default value"

This is the error i am getting:

"C:\Users\Arpit\PycharmProjects\Complete Test\venv\Scripts\python.exe" "C:/Users/Arpit/PycharmProjects/Complete Test/test_for_timeout.py"
enterFatal Python error: could not acquire lock for <_io.BufferedReader name='<stdin>'> at interpreter shutdown, possibly due to daemon threads
Python runtime state: finalizing (tstate=00000000003CAE20)

Thread 0x00000c18 (most recent call first):
  File "C:\Users\Arpit\AppData\Local\Programs\Python\Python38\lib\threading.py", line 1027 in _wait_for_tstate_lock
  File "C:\Users\Arpit\AppData\Local\Programs\Python\Python38\lib\threading.py", line 1015 in join
  File "C:\Users\Arpit\PycharmProjects\Complete Test\venv\lib\site-packages\func_timeout\StoppableThread.py", line 126 in run
  File "C:\Users\Arpit\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932 in _bootstrap_inner
  File "C:\Users\Arpit\AppData\Local\Programs\Python\Python38\lib\threading.py", line 890 in _bootstrap

Thread 0x0000106c (most recent call first):
  File "C:/Users/Arpit/PycharmProjects/Complete Test/test_for_timeout.py", line 7 in doit
  File "C:\Users\Arpit\PycharmProjects\Complete Test\venv\lib\site-packages\func_timeout\dafunc.py", line 68 in funcwrap
  File "C:\Users\Arpit\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870 in run
  File "C:\Users\Arpit\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932 in _bootstrap_inner
  File "C:\Users\Arpit\AppData\Local\Programs\Python\Python38\lib\threading.py", line 890 in _bootstrap

Current thread 0x00001850 (most recent call first):
<no Python frame>

Process finished with exit code 3
Arpit Shahi
  • 1
  • 1
  • 2
  • Is this resolving you issue: https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout – Amiram Jan 15 '20 at 13:44
  • Traceback (most recent call last): File "C:/Users/Arpit/PycharmProjects/Complete Test/test.py", line 7, in i, o, e = select.select( [sys.stdin],[],[], 30 ) OSError: [WinError 10038] An operation was attempted on something that is not a socket – Arpit Shahi Jan 16 '20 at 04:44

1 Answers1

0

If you don't have any limitation you are try the asyncio package.

There is an asynchronous package aioconsole, it\' allow to wait for a user input while performing other tasks asynchronously.

In order to install this package please run: pip install aioconsole

The following code will wait 30 seconds for the user input. If the user didn't input anything at this time it will continue (raise asyncio.TimeoutError) and set the value to the default one.

import asyncio
import aioconsole

async def main(main_loop):
    t = main_loop.create_task(aioconsole.ainput())
    try:
        await asyncio.wait_for(t, timeout=30)
        value = t.result()
    except asyncio.TimeoutError as _:
        value = "default value"
    print(value)
    return value

if __name__ == '__main__':
    l = asyncio.get_event_loop()
    value = l.run_until_complete(main(l))
    l.close()


Regarding your code, I have tried it and it worked for me with some minor changes:

from func_timeout import func_timeout, FunctionTimedOut
def doit():
    value = input("enter")
    return value  # Need to return the value.
try:
    value = func_timeout(5, doit)  # Set the return value to the same variable name
except FunctionTimedOut as _:
    value = "default value"
print(value)
Amiram
  • 1,227
  • 6
  • 14
  • Thanks, the first code worked for me but the second one is showing this error: Fatal Python error: could not acquire lock for <_io.BufferedReader name=''> at interpreter shutdown, possibly due to daemon threads Python runtime state: finalizing (tstate=000000000045AF50) – Arpit Shahi Jan 22 '20 at 10:07
  • By the way my platform is windows 7 – Arpit Shahi Jan 22 '20 at 10:08
  • I would suggest to use the first method (code) due to the fact that it uses built in `python3` functions/abilities. Regarding the seconds code, try reading https://stackoverflow.com/questions/45267439/fatal-python-error-and-bufferedwriter it might help you. – Amiram Jan 22 '20 at 11:40
  • can you please tell me how to call the "value" variable outside this function: "async def main(main_loop):" – Arpit Shahi Jan 24 '20 at 06:50
  • I edited the code with how to return the `value` argument back from the `main` function. – Amiram Jan 24 '20 at 07:24