Dextron
Dextron

Reputation: 628

Multiprocessing error when get key input with inputs

I am using the module Inputs to get key input in Python when I run this code below

import inputs

events = inputs.get_key()

if __name__ == '__main__':
    freeze_support()

I get this error:

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'

This only happens when i run it in a python file. If i run it in the python shell i don't get this error.

Upvotes: 0

Views: 327

Answers (2)

Dextron
Dextron

Reputation: 628

So the answer given by Nathaniel Taulbut was what I needed but it didn't run in a loop which is what I needed so I changed the code a bit to run in a loop.

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()

As far as I have tested it works but is there a way I could get this code working without if __name__ == '__main__'

Upvotes: 0

user10997580
user10997580

Reputation:

freeze_support() has to be properly imported and be the first thing run. That would look like this:

from multiprocessing import freeze_support
import inputs

freeze_support()

if __name__ == '__main__':
    events = inputs.get_key()

Upvotes: 1

Related Questions