Python输入库-'NoneType'对象没有属性'terminate'

时间:2018-08-13 11:37:16

标签: python python-3.x multiprocessing

我正在尝试使用inputs库从鼠标,游戏手柄和键盘获取用户输入。

我尝试了以下应从所有设备读取事件的代码:

import inputs

while True:
    for device in inputs.devices:
        for event in device.read():
            print(event)

运行代码时出现问题-我收到以下错误:AttributeError: 'NoneType' object has no attribute 'terminate'

我还尝试读取一个事件:

import inputs

while True:
    for device in inputs.devices:
        event = device.read()
        print(event)

这给了我同样的错误。

我正在使用Python3.6中的inputs==0.4pip

有人知道如何解决此错误吗?

完整追溯:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\python36\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\python36\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\python36\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\python36\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\python36\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\python36\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\David\Documents\GitHub\Bubbles\testing.py", line 5, in <module>
    event = device.read()
  File "C:\python36\lib\site-packages\inputs.py", line 2313, in read
    return next(iter(self))
  File "C:\python36\lib\site-packages\inputs.py", line 2273, in __iter__
    event = self._do_iter()
  File "C:\python36\lib\site-packages\inputs.py", line 2292, in _do_iter
    data = self._get_data(read_size)
  File "C:\python36\lib\site-packages\inputs.py", line 2365, in _get_data
    return self._pipe.recv_bytes()
  File "C:\python36\lib\site-packages\inputs.py", line 2330, in _pipe
    self._listener.start()
  File "C:\python36\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\python36\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\python36\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\python36\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\python36\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "C:\python36\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
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:\python36\lib\site-packages\inputs.py", line 2337, in __del__
  File "C:\python36\lib\multiprocessing\process.py", line 116, in terminate
AttributeError: 'NoneType' object has no attribute 'terminate'

1 个答案:

答案 0 :(得分:0)

使用以下方法定义您的步骤:

def func():
    while True:
        for device in inputs.devices:
        event = device.read()
        print(event)

然后使用以下命令调用函数:

if __name__ == '__main__':
     func()