""process class can also be subclassed just like threading threadqueue works like queue queue but for cross-processnot cross-thread ""import ostimequeue from multiprocessing import processqueue process-safe shared queue queue is pipe locks/semas class counter(process)label @def __init__(selfstartqueue)self state start self post queue process __init__(selfretain state for use in run def run(self)for in range( )time sleep( self state + print(self label ,self pidself stateself post put([self pidself state]print(self labelself pid'-'run in newprocess on start(self pid is this child' pid stdout file is shared by all if __name__ ='__main__'print('start'os getpid()expected post queue( counter( postq counter( postr counter( postp start() start() start(start processes sharing queue children are producers while expectedtime sleep( trydata post get(block=falseexcept queue emptyprint('no data 'elseprint('posted:'dataexpected - parent consumes data on queue this is essentially like guithough guis often use threads join() join() join(print('finish'os getpid() exitcodemust get before join putter exitcode is child exit status notice in this code howthe time sleep calls in this code' producer simulate long-running tasks all four processes share the same output streamprint calls go the same place and don' overlap badly on windows (as we saw earlierthe multiprocessing module also has shareable lock object to synchronize access if requiredthe multiprocessing module