如何为Windows重写这个多处理代码?

2024-04-25 22:23:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用多重处理,以便在运行其他代码时获得用户输入。对于我来说,这个版本的代码在ubuntu19.04上运行,但是对于我的朋友来说,它在windows上不起作用。你知道吗

import getch
import time
from multiprocessing import Process, Queue

prev_user_input = ' '
user_input = ' '


# Getting input from the user
queue = Queue(1)

def get_input():
    char = ' '
    while char != 'x':
        char = getch.getch()
        queue.put(char)

# Starting the process that gets user input
proc = Process(target=get_input)
proc.start()


while True:

    # Getting the users last input
    while not queue.empty():
        user_input = queue.get()

    # Only print user_input if it changes
    if prev_user_input != user_input:
        print(user_input)
        prev_user_input = user_input

    time.sleep(1/10)

如何使此代码在windows上工作?你知道吗

用户输入也落后于一个输入。如果用户按下一个按钮,它只会在他按下另一个按钮后打印。关于如何解决这个问题的解决方案也会有所帮助。你知道吗

编辑1: 他用的是python3.7.4,我用的是3.7.3。你知道吗

我按照建议尝试了这个代码

import msvcrt
import time
from multiprocessing import Process, Queue

prev_user_input = ' '
user_input = ' '


# Getting input from the user
queue = Queue(1)

def get_input():
    char = ' '
    while char != 'x':
        char = msvcrt.getch()
        queue.put(char)

# Starting the process that gets user input
if __name__ == '__main__':
    proc = Process(target=get_input)
    proc.start()


    while True:

        # Getting the users last input
        while not queue.empty():
            user_input = queue.get()

        # Only print user_input if it changes
        if prev_user_input != user_input:
            print(user_input)
            prev_user_input = user_input

        time.sleep(1/10)

但没有打印任何字符。你知道吗

编辑2: 我在windows上使用^{}模块,在ubuntu上使用^{}模块。很抱歉刚才在帖子里没有说清楚。你知道吗


Tags: the代码fromimportinputgetiftime
1条回答
网友
1楼 · 发布于 2024-04-25 22:23:47

以下是我在Windows上的工作。它包含了我在您的问题下的评论中建议的所有更改,包括关于独立内存空间的最后一个更改。你知道吗

类似的东西应该也可以在ubuntu下使用它的getch()版本,尽管我还没有测试过它。在主进程上,创建Queue并将其作为参数传递给get_input()目标函数,以便它们使用相同的对象交换数据。你知道吗

我还decode()msvcrt.getch()返回的bytes对象,将其转换为(1个字符)Unicode UTF-8字符串。你知道吗

import msvcrt
import time
from multiprocessing import Process, Queue

prev_user_input = ' '
user_input = ' '


def get_input(queue):
    char = ' '
    while char != b'x':
        char = msvcrt.getch()
        queue.put(char.decode())  # Convert to utf-8 string.

if __name__ == '__main__':
    # Getting input from the user.
    queue = Queue(1)

    # Starting the process that gets user input.
    proc = Process(target=get_input, args=(queue,))
    proc.start()


    while True:

        # Getting the users last input
        while not queue.empty():
            user_input = queue.get()

        # Only print user_input if it changes
        if prev_user_input != user_input:
            print(user_input)
            prev_user_input = user_input

        time.sleep(1/10)

更新

要隐藏操作系统差异并使代码更具可移植性,您可以执行如下所示的import操作,这也允许您像在问题代码中那样定义get_input()函数:

import os
import time
from multiprocessing import Process, Queue

try:
    import msvcrt
    getch = msvcrt.getwch  # Wide char variant of getch() that returns Unicode.
except ModuleNotFoundError:  # Not Windows OS - no msvcrt.
    from getch import getch

prev_user_input = ' '
user_input = ' '


def get_input(queue):
    char = ' '
    while char != 'x':
        char = getch()
        queue.put(char)


if __name__ == '__main__':
    # For getting input from the user.
    queue = Queue(1)

    # Starting the process that gets user input.

    .
    .
    .

相关问题 更多 >

    热门问题