0

In short I am trying to run a function (loop) with variables, say 'val', along another function that gets user input for 'val'. The user will input an initial value but then can change the value and the loop will run uninterrupted and adopt this new value of 'val'.

I know I need to use multiprocessing, and queues, and also stdin, seen here: Python user input in child process. Although I cannot seem to get the processes to run. I have found examples on each part that work, but together I cannot get them to function.

from multiprocessing import Process, Queue
import time
import sys
import os

def reader(q):
    while True:
        #do some stuff 
        if q.empty() == False: #Code gets hung here
            msg = q.get(block = False)  #These few lines will change       
            if (msg == 10):     #Not sure of syntax with this so going basic
            print('Value:'+str(msg))
            break


if __name__=='__main__':

    q = Queue()   

    reader_p = Process(target=reader, args=((q),))
    reader_p.daemon = True
    reader_p.start()
    while True:
        msg = input("Enter: ")
        q.put(msg)

    reader_p.join()

*NOTE: I set this shorter by using q.get() != false: but again was not sure of syntax.

The issue seems to be in q.get() when the program gets to q.get(block = False) that is then the program stops and issues me a queue.empty error. With my edit the code is now getting hung where noted. If this is taken out the errors mentioned occur.

Python is not my best language and I am not familiar with Queues so this is not something I have a great understanding of, so I am hoping you can fix any of my syntax errors or point out if I am using any functions improperly.

Edit: It was mentioned that Threads may be the best bet, based on the original wording. But I will say that the actual code I am using uses 2+ loops (for now) and needs to run on a single variable.

Psudo Code:

process 1:
    get input
    infinte loop:
        do something with input variable  
        #this is on a rasp pi and is just going to output onto display                  
        #it needs to run on infinite loop till program is killed 

precess 2:
    check for new input
    new input is different than last/first input? 
         send input to process #1 
Nick Nash
  • 1
  • 3
  • I don't know your use case of course, but as the problem is stated, I would say it sounds more like you need different threads, than different processes. – JohanL Jun 22 '17 at 04:50
  • @JohanL I see where my wording may have been unclear, and my apologies for using unclear sample code. But unless I am mistaken I do need processes as I only want the new variable to be passed at a certain instance. Along with this more than 2 processes (or threads) will be used. If this can be fixed with treads this would be an over site on my part and I would enjoy help as to how to make this happen with treads! – Nick Nash Jun 22 '17 at 05:27
  • Will the processes not run on the same hardware? Or are both running on the same Raspberry Pi? – JohanL Jun 22 '17 at 06:36
  • They are going to run on the same RPi, one script. – Nick Nash Jun 22 '17 at 12:12
  • Do you have a connected keyboard or how do you provide input? If you only have one process handling user input, it is probably best to let that be the main process and then handle calculations in other processes, as the main process automatically owns stdin and stdout. – JohanL Jun 22 '17 at 12:44
  • Input would be through a keyboard, I edited my code using your suggestion about getting input in the main process so I was able to cut out all the stdin code but I am still getting the same 'queue.empty' error. – Nick Nash Jun 22 '17 at 16:24
  • Are you running Python 2.7 or Python 3.x? Your code, if correctly indented works at my computer (albeit not an RPi) want Python 2.7. But, of course sending 10 will only terminate the subprocess and not the main one, making new values fill up the queue. – JohanL Jun 22 '17 at 18:47

0 Answers0