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