简体   繁体   English

致命的 Python 错误:无法为 &lt;_io.BufferedReader name=&#39; 获取锁<stdin> &#39;&gt; 在解释器关闭时,可能是由于守护线程

[英]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.

相关问题 致命的 Python 错误:在解释器关闭时无法获取 &lt;_io.BufferedReader name=8&gt; 的锁定,可能是由于守护线程 - Fatal Python error: could not acquire lock for <_io.BufferedReader name=8> at interpreter shutdown, possibly due to daemon threads 如何修复“致命 Python 错误:_enter_buffered_busy:无法获取 &lt;_io.BufferedWriter name=”的锁定<stdout> '&gt; 在解释器关闭时' 错误?</stdout> - How to fix a 'fatal Python error: _enter_buffered_busy: could not aquire lock for <_io.BufferedWriter name='<stdout>'> at interpreter shutdown' error? 在Python2中从sys.stdin创建io.BufferedReader - Making io.BufferedReader from sys.stdin in Python2 解开“ _io.BufferedReader”错误 - unpickling '_io.BufferedReader' error ResourceWarning:未关闭的文件 &lt;_io.BufferedReader name=4&gt; - ResourceWarning: unclosed file <_io.BufferedReader name=4> Python - 使用 _io.BufferedReader 获取 TypeError - Python - Getting TypeError with _io.BufferedReader &#39;io.BufferedReader&#39;对象不可下标&#39;错误 - 'io.BufferedReader' object is not subscriptable' error Python3:Reportlab图像 - ResourceWarning:未闭合文件&lt;_io.BufferedReader name = ...&gt; - Python3: Reportlab Image - ResourceWarning: unclosed file <_io.BufferedReader name=…> 尝试在 Python 中使用 matplotlib 保存图形动画 - “无效的文件对象:&lt;_io.BufferedReader name=76&gt;” - Trying to save an animated of graph with matplotlib in Python - "Invalid file object: <_io.BufferedReader name=76>" Pyqt5线程无法正常工作(致命的Python错误:无法获取锁) - Pyqt5 threading can't work (Fatal Python error: could not acquire lock)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM