如何在没有EOFError的python中发生线程的情况下获取用户输入? [英] How can i get userinput in a thread without EOFError occuring in python?

查看:72
本文介绍了如何在没有EOFError的python中发生线程的情况下获取用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to receive/send data at the same time, and my idea to doing this was

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()替换的服务器上获取数据,并且我想要一个线程,而另一个人不断检查数据时,正在获得输入.我希望它看起来像:

I hope you see what I am trying to do but I will explain it some more: 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. I would like it to look somewhat like:

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

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

But I have no Idea how to get it done. If I replace the queue.put(input()) with another queue.put(random.randint(0, 50)) the printing in the two lines will work as expected, but

我怎么在底部有一个输入字段",

how can I have an 'input field' in the bottom and

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

how can I get the Input without the EOF?

推荐答案

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

Looks like, according to your description: 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. what you really want to use is multi-threading, but in your code your are creating and executing 2 new Processes, instead of 2 new Threads. So, if what you want to use is multi-threading, do the following instead, replacing the use of multi-processing by the use of multi-threading:

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

The instances of queue.Queue are thread-safe, so they can safely be used by multi-thread code, like in the code.

这篇关于如何在没有EOFError的python中发生线程的情况下获取用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆