def procs(sig): # print 'procs process id:', os.getpid() ## CHILD OF __MAIN__ ## 1 = INIT DAEMONS ## 2 = CONNECTED DAEMONS ## 5 = CHECK PROCS ## 9 = DISTROY ## a parent pid can only test its children global sock global connected global pData global pReport global pCheckin debug(2, 'PROCS', 'CALLED FROM {}'.format(__name__)) debug(3, 'PROCS', 'CALLED WITH SIGNAL {}'.format(sig)) sig = int(sig) if sig == int(1): try: if connected == True and sock == True: try: pData = Process(target=data_recv, ) pData.name = 'pData' pData.start() try: pid3 = pData.pid() log(4, 'pData Started With PID {}'.format(pid3)) except: log(4, 'pData Started') pass except: log(4, 'pData Refused To Start') raise else: log(4, 'pData Not Connected') except KeyboardInterrupt: signals(2, 'PROCS 1', 'SIGINT') raise except: signals(4, 'PROCS 1', 'PDATA REFUSED TO START') raise elif sig == int(2): try: if connected == True and sock == True: try: pCheckin = Process(target=checkin, args=('CLIENT', 'PROCS', 'PING', connected, sock)) pCheckin.name = 'pCheckin' pCheckin.start() try: pid4 = pCheckin.pid() log(4, 'pCheckin Started With PID {}'.format(pid4)) except: log(4, 'pCheckin Started') pass except: log(4, 'pCheckin Refused To Start') raise else: log(4, 'pCheckin Not Connected') except KeyboardInterrupt: signals(2, 'PROCS 2', 'SIGINT') raise except: signals(4, 'PROCS 2', 'CHECKIN FAILED TO START') raise try: if connected == True and sock == True: try: pReport = Process(target=genfakereport, args=()) pReport.name = 'pReport' pReport.start() try: pid5 = pReport.pid() log(4, 'pReport Started With PID {}'.format(pid5)) except: log(4, 'pReport Started') pass except: log(4, 'pReport Refused To Start') raise else: log(4, 'pReport Not Connected') except KeyboardInterrupt: signals(2, 'PROCS 2', 'SIGINT') except: signals(4, 'PROCS 2', 'PREPRT FAILED TO START') raise elif sig == int(5): connstatus = 'CONNECTED {} SOCKET {}'.format(connected, sock) log( 4, 'PROCS CALLED FROM {} WITH SIGNAL {} AND {}'.format( __name__, sig, connstatus)) # print (pData, pData.is_alive()) # print (pReport, pReport.is_alive()) elif sig == int(9): try: connstatus = 'CONNECTED {} SOCKET {}'.format(connected, sock) log( 4, 'PROCS CALLED FROM {} WITH SIGNAL {} AND {}'.format( __name__, sig, connstatus)) log(4, 'START KILLING PIDS') pData.terminate() pReport.terminate() pReport.terminate() log(4, 'WHICH PIDS ARE STILL ALIVE?????') procs(5) except KeyboardInterrupt: time.sleep(0.5) signals(2, 'PROCS 9', 'SIGINT') raise except: time.sleep(0.5) raise
def connect(sig, who): ## 1 = CONNECT ## 2 = WITH SOCKET ## 3 = CONNECTED ## 4 = RECONNECT global q global s global pMain global packet_send global packet_recv global init_packet_send global init_packet_recv global sock global connected debug(2, 'CONNECT', 'CALLED') debug(3, 'CONNECT', 'SIGNAL {}'.format(sig)) debug(3, 'CONNECT', 'WHO {}'.format(who)) debug(3, 'CONNECT', 'SOCKET {}'.format(sock)) debug(3, 'CONNECT', 'CONNECTED {}'.format(connected)) sig = int(sig) if sig == int(1) and sock == False: try: connsock(1, 'CONNECT') except KeyboardInterrupt: signals(2, 'CONNECT 1', 'SIGINT') raise except: raise elif sig == int(2) and sock == True: try: connsock(2, 'CONNECT') except KeyboardInterrupt: signals(2, 'CONNECT 2', 'SIGINT') raise except: raise elif sig == int(3) and sock == True and connected == True: stdout(1, 'Connected to {}.'.format(host)) packet_recv = int(0) packet_send = int(0) init_packet_recv = int(0) init_packet_send = int(0) try: log(4, 'Init Starting') comms(1, None) log(4, 'Start Some Workers For Queue') try: pool = multiprocessing.Pool(processes=1) m = multiprocessing.Manager() q = m.Queue() workers = pool.apply_async(localqueue, ('GET', None)) except KeyboardInterrupt: signals(2, 'CONNECT 3', 'SIGINT') raise except: print 'unable to start workers' p = Process(target=localqueue, args=('GET', None)) p.name = 'comms2' p.start() raise log(4, 'BUILD CREATED') procs(1) log(4, 'Init Done') except KeyboardInterrupt: signals(2, 'Init', 'SIGINT') raise except: raise try: log(4, 'Main Starting') pMain = Process(target=procs, args=('2')) pMain.name = 'pMain' pMain.start() time.sleep(2) try: pid2 = pMain.pid() log(4, 'pMain Started With PID {}'.format(pid2)) except: log(4, 'pMain Started') log(4, 'Main Done') except KeyboardInterrupt: signals(2, 'CONNECT pMain', 'SIGINT') raise except: signals(4, 'CONNECT pMain', None) raise time.sleep(3) try: stdin() except KeyboardInterrupt: signals(2, 'CONNECT STDIN', 'SIGINT') raise except: signals(4, 'CONNECT STDIN', None) raise elif sig == int(4): print 'HELLO WORLD LINE 743' #connsock(9, 'CONNECT') #log(4, 'CONNECTION LOST!') sock = False connected = False
logger.debug("Starting main loop") if __name__ == '__main__': logger.warning('Starting Main Process: %i', os.getpid()) info('main line') # Start process # logger.warning("Cache prefix: %s", cachefile) try: logger.debug('Starting pControl') p = Process(target=pcontrol.pControl, args=(org, place, brokerIP, clientId, cachefile)) p.start() logger.warning('Starting pControl, PID: %s', str(p.pid())) # o = Process(target=airsensor.airSensor, args=(org, place, brokerIP, clientId, cfgfile)) # o.start() while True: if not p.is_alive(): logger.warning('pControl is DEAD - Restarting-it') p.terminate() p.run() time.sleep(0.1) logger.warning("New PID: %s", str(p.pid)) ''' if not o.is_alive(): logger.warning('airSensor is DEAD - Restarting-it') o.terminate() o.run()