Skip to content
Advertisement

How to rewrite this multiprocessing code for Windows?

I’m currently using multiprocessing so I can obtain user input while running other code. This version of code runs on ubuntu 19.04 for me, but for my friend it doesn’t work on windows.

JavaScript

How can I make this code work on windows?

Also the user input lags behind by one input. If the user presses a button it only prints after he pushes another button. Solutions on how to fix this would also help.

Edit 1: He’s using Python 3.7.4 and I’m using 3.7.3.

I tried this code as suggested

JavaScript

But no characters were printed.

Edit 2: I’m using msvcrt module on windows and the getch module on ubuntu. Sorry for not making that clear earlier in the post.

Advertisement

Answer

The following works for me on Windows. It incorporates all the changes I suggested in my comments under your question, including the final one about separate memory-spaces.

Something similar should also work under ubuntu using its version of getch(), although I haven’t tested it. on The main process creates the Queue and passes it as an argument to the get_input() target function so they’re both using the same object to exchange data.

I also decode() the bytes object returned from msvcrt.getch() to convert it into a (1 character) Unicode UTF-8 string.

JavaScript

Update

To hide the OS differences and make the code more portable, you could do the importing as shown below, which would also allow you to define the get_input() function more like you did in the code in your question:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement