Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为Windows重写此多处理代码?_Python_Python 3.x_Linux_Windows_Multiprocessing - Fatal编程技术网

Python 如何为Windows重写此多处理代码?

Python 如何为Windows重写此多处理代码?,python,python-3.x,linux,windows,multiprocessing,Python,Python 3.x,Linux,Windows,Multiprocessing,我目前正在使用多处理,以便在运行其他代码时获得用户输入。对于我来说,这个版本的代码在ubuntu 19.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_i

我目前正在使用多处理,以便在运行其他代码时获得用户输入。对于我来说,这个版本的代码在ubuntu 19.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: 他使用的是Python 3.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上的模块。很抱歉在文章的前面没有说清楚。

下面的内容对我来说在Windows上很有用。它包含了我在你的问题下的评论中建议的所有更改,包括最后一个关于独立内存空间的更改

类似的东西也可以在ubuntu下使用其版本的
getch()
,尽管我还没有测试过它。在主进程上,创建
队列
,并将其作为参数传递给
get\u input()
目标函数,以便它们都使用相同的对象来交换数据

我还将
decode()
msvcrt.getch()
返回的对象转换为(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)
更新 要隐藏操作系统的差异并使代码更易于移植,您可以执行如下所示的
导入
ing,这也允许您像在问题代码中一样定义
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.

    .
    .
    .

尝试在
之前添加
行,如果
行的
名称行,则开始获取用户输入的过程行-同时将该行后面的所有代码缩进。“主模块的安全导入”小节中的
多处理
模块对此进行了说明。您在每台机器上使用的python版本是什么?@martineau我尝试过这样做。现在该程序没有崩溃,但它没有显示与linux中相同的行为,即打印字符。@AnnKilzer我使用的是3.7.3,他使用的是3.7.4。我不明白使用
msvcrt.getch()
如何在Windows以外的任何系统上工作,但你声称它在ubuntu上工作。这个解决方案有效,非常感谢你。在“x”之前的b点是什么?另外,为什么在使用char.decode()时,打印字符没有任何延迟,而在不使用它时却没有延迟?同样,char.decode()在windows上也能完美地工作,但在ubuntu上我遇到了一个错误
AttributeError:'str'对象没有属性“decode”
为什么?字符串前缀
b
表示文本是内置类的实例,该类是值
msvcrt.getch()返回的类型。
decode()
调用将其转换为utf-8 Unicode编码的常规Python字符串。如果您使用的是
getch()
“wide”char变量,则这些都不是必需的,因为它返回Unicode。显然,ubuntu上的
getch()
返回utf-8字符串,因此它也不需要这样做。