'''
An SIMPLE, BRUTAL Demostration for multiprocessing on Python3.
Pipe used for interprocess communications.
Tips:
Run this demo in a shell to get the messages printed.
This demo shows nothing if you run it in idle3 or spyder3.
'''
from multiprocessing import Process, Pipe
import time
def Motion(conn_motion):
'''
Data generator/sender 1.
'''
while True:
s = "motion function %s"%time.ctime()
print('>>>>', s)
conn_motion.send(s)
time.sleep(1.3)
def GPS(conn_gps):
'''
Data generator/sender 2.
'''
while True:
s = "gps function %s"%time.ctime()
print('>>>>', s)
conn_gps.send(s)
time.sleep(2.3)
def Worker(conn_motion, conn_gps):
'''
Data receiver/processer.
'''
while True:
print('-'*40)
if conn_motion.poll(): # to avoid blocking of recv.
s = conn_motion.recv()
print('<<<<', s)
if conn_gps.poll():
s = conn_gps.recv()
print('<<<<', s)
time.sleep(0.1)
def Main():
conn_motion_1, conn_motion_2 = Pipe()
conn_gps_1, conn_gps_2 = Pipe()
p_motion = Process(target=Motion, args=(conn_motion_1,))
p_gps = Process(target=GPS, args=(conn_gps_1,))
p_worker = Process(target=Worker, args=(conn_motion_2, conn_gps_2,))
p_motion.start()
p_gps.start()
p_worker.start()
p_motion.join()
p_gps.join()
p_worker.join()
if __name__ == '__main__':
Main()