如何在线程中获得用户输入而不会在python中出现EOFError



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

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

我怎么能在底部和中有一个"输入字段">

如果没有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

队列的实例。队列是线程安全的,因此多线程代码可以安全地使用它们,就像在代码中一样。

相关内容

  • 没有找到相关文章

最新更新