如何在线程中获取userinput而不在python中出现eoferor?

如何在线程中获取userinput而不在python中出现eoferor?,python,multithreading,command-line,multiprocessing,python-multiprocessing,Python,Multithreading,Command Line,Multiprocessing,Python Multiprocessing,我试图同时接收/发送数据,我的想法是 导入多处理 导入时间 从重印输入输出 导入时间 随机输入 def接收线程(队列): 尽管如此: queue.put(random.randint(0,50)) 睡眠时间(0.5) def sendThread(队列): 尽管如此: queue.put(输入()) 如果名称=“\uuuuu main\uuuuuuuu”: 发送队列=多处理。队列() 接收队列=多处理。队列() send_thread=multiprocessing.Process(target

我试图同时接收/发送数据,我的想法是

导入多处理
导入时间
从重印输入输出
导入时间
随机输入
def接收线程(队列):
尽管如此:
queue.put(random.randint(0,50))
睡眠时间(0.5)
def sendThread(队列):
尽管如此:
queue.put(输入())
如果名称=“\uuuuu main\uuuuuuuu”:
发送队列=多处理。队列()
接收队列=多处理。队列()
send_thread=multiprocessing.Process(target=sendThread,args=[send_queue],)
receive\u thread=multiprocessing.Process(target=receiveThread,args=[receive\u queue],)
receive_thread.start()
发送线程。开始()
将输出(初始长度=2,间隔=0)作为输出线:
尽管如此:
输出_行[0]=“已接收:{}”。格式(str(receive_queue.get())
输出_行[1]=“上次发送:{}”。格式(str(send_queue.get())
#output_lines[2]=“Input:{}”.format()我不知道怎么做
#还将数据存储在文件中,但这与此处无关
然而,这会导致

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())
,两行中的打印将按预期工作,但是

如何在底部和底部设置“输入字段”


如果没有EOF,我如何获取输入?

根据您的描述,看起来像:
我想要一个线程从我用random.randint()替换的服务器获取数据,我想要一个线程在另一个线程不断检查数据时,正在获取输入。
您真正想要使用的是多线程,但在您的代码中,您正在创建和执行两个新进程,而不是两个新线程。因此,如果要使用多线程,请执行以下操作,用多线程代替多处理:

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的实例是线程安全的,因此它们可以被多线程代码安全地使用,就像在代码中一样。

但是这并不能解决EOFError问题,不是吗?我现在在手机上,所以我无法检查,但这仍然不起作用。我在我的电脑上运行了它,它没有引发EOF异常,你应该自己尝试一下。不管怎样,我尝试了你的解决方案,它起作用了,唯一的问题是它暂停了我的代码,也就是说,我没有从更新的输出中获得更新,它也不只是更新了2行,而是每次按ENTER键都会打印新的2行。这有什么实质性区别?虽然这一个看起来更好,但两个似乎都在问同一个问题。这能回答你的问题吗@mistermiyagi问题是一样的,问题不是。我有两个线程运行的问题,我解决了我现在的问题是EOF。你知道的比我多,我应该编辑它吗?我在这里还是相当新的,所以idk stackoverflow是最佳实践。我回家后会试试你贴的链接,谢谢!