[英]Fatal Python error: could not acquire lock for <_io.BufferedReader name='<stdin>'> at interpreter shutdown, possibly due to daemon threads
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我想写一个python脚本,如果我没有通过输入命令输入任何值,那么它应该在30秒后为我的变量分配一个默认值,我的平台是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.如果您没有任何限制,您可以尝试使用asyncio包。
There is an asynchronous package aioconsole
, it\\' allow to wait for a user input while performing other tasks asynchronously.有一个异步包
aioconsole
,它允许在异步执行其他任务的同时等待用户输入。
In order to install this package please run: pip install aioconsole
为了安装这个包,请运行:
pip install aioconsole
The following code will wait 30 seconds for the user input.以下代码将等待用户输入 30 秒。 If the user didn't input anything at this time it will continue (
raise asyncio.TimeoutError
) and set the value to the default one.如果用户此时没有输入任何内容,它将继续(
raise asyncio.TimeoutError
)并将值设置为默认值。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.