当我在下面运行此代码时,我正在使用Inputs模块获取Python中的键输入
import inputs
events = inputs.get_key()
if __name__ == '__main__':
freeze_support()
我收到此错误:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
Exception ignored in: <bound method InputDevice.__del__ of inputs.Keyboard("/dev/input/by-id/usb-A_Nice_Keyboard-event-kbd")>
Traceback (most recent call last):
File "C:\Users\26099\AppData\Local\Programs\Python\Python36\lib\site-packages\inputs.py", line 2541, in __del__
File "C:\Users\26099\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 113, in terminate
AttributeError: 'NoneType' object has no attribute 'terminate'
仅当我在python文件中运行它时才会发生。如果我在python shell中运行它,则不会收到此错误。
答案 0 :(得分:1)
freeze_support()
必须正确导入并首先运行。看起来像这样:
from multiprocessing import freeze_support
import inputs
freeze_support()
if __name__ == '__main__':
events = inputs.get_key()
答案 1 :(得分:1)
Nathaniel Taulbut给出的答案是我需要的,但是它并没有在我需要的循环中运行,所以我稍稍更改了代码以使其在循环中运行。
from multiprocessing import freeze_support
import inputs
freeze_support()
def Keys():
if __name__ == '__main__':
while True:
events = inputs.get_key()
for event in events:
print(event.code)
Keys()
据我测试,它可以工作,但是有一种方法可以使该代码在没有if __name__ == '__main__'
的情况下工作