如何在 python 中不发生 EOFError 的情况下在线程中获取用户输入?

How can i get userinput in a thread without EOFError occuring in python?

我正在尝试同时 receive/send 数据,我的想法是

import multiprocessing
import time
from reprint import output
import time
import random

def receiveThread(queue):
    while True:
        queue.put(random.randint(0, 50))
        time.sleep(0.5)

def sendThread(queue):
    while True:
        queue.put(input())


if __name__ == "__main__":
    send_queue = multiprocessing.Queue()
    receive_queue = multiprocessing.Queue()

    send_thread = multiprocessing.Process(target=sendThread, args=[send_queue],)
    receive_thread = multiprocessing.Process(target=receiveThread, args=[receive_queue],)
    receive_thread.start()
    send_thread.start()

    with output(initial_len=2, interval=0) as output_lines:
        while True:
            output_lines[0] = "Received:  {}".format(str(receive_queue.get()))
            output_lines[1] = "Last Sent: {}".format(str(send_queue.get()))
            #output_lines[2] = "Input: {}".format() i don't know how
            #also storing the data in a file but that's irrelevant for here

然而这会导致

Received:  38                                                                                                Process Process-1:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/mge/repos/python/post_ug/manual_post/main.py", line 14, in sendThread
    queue.put(input())
EOFError: EOF when reading a line

我希望你明白我正在尝试做什么,但我会再解释一下:我想要一个线程从我用 random.randint() 替换的服务器获取数据,我想要一个线程正在获取输入,而另一个线程则不断检查数据。我希望它看起来有点像:

Received: 38              Received: 21                     Received: 12
Last Sent:         =>     Last Sent: Hello World!    =>    Last Sent: Lorem Ipsum   => ...
Input: Hello Wo           Input: Lore                      Input:

但我不知道如何完成它。如果我用另一个 queue.put(random.randint(0, 50)) 替换 queue.put(input()) ,两行中的打印将按预期工作,但是

我怎样才能在底部有一个'input field'和

如何在没有 EOF 的情况下获取输入?

根据您的描述,看起来像:I want one thread that gets data from a server that I have replaced with the random.randint(), and I want one thread that, while the otherone is constantly checking for the data, is getting an input.您真正想要使用的是多线程,但在您的代码中,您正在创建和执行 2 个新进程,而不是 2 个新线程。因此,如果您想使用多线程,请改为执行以下操作,将多处理的使用替换为多线程的使用:

from queue import Queue
import threading
import time
from reprint import output
import time
import random


def receiveThread(queue):
    while True:
        queue.put(random.randint(0, 50))
        time.sleep(0.5)


def sendThread(queue):
    while True:
        queue.put(input())


if __name__ == "__main__":
    send_queue = Queue()
    receive_queue = Queue()

    send_thread = threading.Thread(target=sendThread, daemon=True, args=(send_queue,))
    receive_thread = threading.Thread(target=receiveThread, daemon=True,  args=(receive_queue,))
    receive_thread.start()
    send_thread.start()

    with output(initial_len=2, interval=0) as output_lines:
        while True:
            output_lines[0] = "Received:  {}".format(str(receive_queue.get()))
            output_lines[1] = "Last Sent: {}".format(str(send_queue.get()))
            #output_lines[2] = "Input: {}".format() i don't know how
            #also storing the data in a file but that's irrelevant for here

queue.Queue的实例是线程安全的,所以它们可以安全地被多线程代码使用,就像在代码中一样。