问题
I have an Arduino which sends a JSON packet to a Python process (PP1). This Python process will run continuously. But this process has to invite and receive JSON packets to another Python process (PP2). Basically PP1 has to pass the JSON packet received from Arduino to PP2. And PP1 has to receive commands packets from PP2 (can be in JSON format too).
Link to architecture image:
Bellow a begin the code of Python process 1
import json
#open port
serialport = serial.Serial('COM5',38400,timeout=1,stopbits=serial.STOPBITS_TWO);
time.sleep(1);
#loop
while(True):
#receive arduino data
receive = serialport.readline()
#vparse json
try:
test = json.loads(receive)
except:
print Exception.message
else:
print json.dumps(test)
Do you know a simple way to do this? Is multithreading necessary?
回答1:
You'll need "somewhere" to put your data. The most simple solution would be using multiprocessing.Queue
, if you need to scale maybe you can look into some job queue (huey, celery, django_q).
An example using multiprocessing.Queue
:
import multiprocessing
def pp1(q, data):
processed_data = data_processing(data) # do some data processing and its result
q.put(processed_data)
def pp2(q):
result = q.get()
show_results(result) # show results to the user
if __name__ == '__main__':
queue = multiprocessing.Queue()
process_1 = multiprocessing.Process(target=pp1, args=(queue,))
process_1.start()
process_2 = multiprocessing.Process(target=pp2, args=(queue,))
process_2.start()
Your loops would be the data_processing
and show_results
(dummy) functions.
You can read more about process communication here: https://pymotw.com/3/multiprocessing/communication.html
Hope it helps
来源:https://stackoverflow.com/questions/45066782/exchange-data-between-two-python-processes