0

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?

Maritn Ge
  • 997
  • 8
  • 35
  • 1
    How does this substantially differ from [your earlier question](https://stackoverflow.com/questions/64994872/how-can-i-properly-run-2-threads-at-the-same-time-that-await-things-to-happen-in)? While this one appears to be in better shape, both appear to ask about the same issue. – MisterMiyagi Dec 08 '20 at 18:47
  • Does this answer your question? [Python multiprocessing stdin input](https://stackoverflow.com/questions/30134297/python-multiprocessing-stdin-input) – MisterMiyagi Dec 08 '20 at 18:54
  • @mistermiyagi the issue is the same, the problem not. I had issues gettinh both threads to run, i solved that the problem I have now is the EOF. You know more about this than I do though, should I just have edited it? I'm still fairly new here and all so idk stackoverflow best practice. I'll try the link you posted when I'm home, thank you! – Maritn Ge Dec 08 '20 at 20:27

1 Answers1

-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.

  • this however won't solve the EOFError, will it? I'm on mobile now so I can't check, but this will still not work. – Maritn Ge Dec 08 '20 at 12:23
  • I ran it in my my PC, and it's not throwing EOF Exception, you should try it by yourself – Reynier Garcia Dec 08 '20 at 12:44
  • anyway, I tried your solution and it works, the only problem is that it pauses my code, aka i don't get the update from the output updated, it also doesn't have just the 2 lines that it updates but prints new 2 lines every time i press enter – Maritn Ge Dec 09 '20 at 12:03