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
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)
User contributions licensed under CC BY-SA 3.0