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

一世执手 提交于 2021-02-20 03:51:21

问题


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

This however results in

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

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:

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

how can I get the Input without the EOF?


回答1:


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

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



来源:https://stackoverflow.com/questions/65196869/how-can-i-get-userinput-in-a-thread-without-eoferror-occuring-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!