Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Esecuzione di più thread
È possibile eseguire la logica dell'applicazione su un thread di elaborazione e utilizzare altri thread per altri processi in background. Ad esempio, puoi creare un threadserve il traffico HTTPper il debug o un thread che monitora i risultati dell'inferenza e invia dati aAWS.
Per eseguire più thread, si utilizza ilModulo di threading
Esempio Pacchetti/123456789012-debug_server-1.0/application.py Loop principale
def main(): panorama = panoramasdk.node() while True: try: # Instantiate application logger.info('INITIALIZING APPLICATION') app = Application(panorama) # Create threads for stream processing, debugger, and client app.run_thread = threading.Thread(target=app.run_cv) app.server_thread = threading.Thread(target=app.run_debugger) app.client_thread = threading.Thread(target=app.run_client) # Start threads logger.info('RUNNING APPLICATION') app.run_thread.start() logger.info('RUNNING SERVER') app.server_thread.start() logger.info('RUNNING CLIENT') app.client_thread.start() # Wait for threads to exit app.run_thread.join() app.server_thread.join() app.client_thread.join() logger.info('RESTARTING APPLICATION') except: logger.exception('Exception during processing loop.')
All'uscita di tutti i thread, l'applicazione si riavvia automaticamente. Larun_cv
loop elabora le immagini dai flussi di telecamere. Se riceve un segnale di arresto, interrompe il processo di debugger, che esegue un server HTTP e non può arrestarsi. Ogni thread deve gestire i propri errori. Se un errore non viene rilevato e registrato, il thread esce silenziosamente.
Esempio Pacchetti/123456789012-debug_server-1.0/application.py Loop di elaborazione
# Processing loop def run_cv(self): """Run computer vision workflow in a loop.""" logger.info("PROCESSING STREAMS") while not self.terminate: try: self.process_streams() # turn off debug logging after 15 loops if logger.getEffectiveLevel() == logging.DEBUG and self.frame_num == 15: logger.setLevel(logging.INFO) except: logger.exception('Exception on processing thread.') # Stop signal received logger.info("SHUTTING DOWN SERVER") self.server.shutdown() self.server.server_close() logger.info("EXITING RUN THREAD")
I thread comunicano tramite l'applicazioneself
oggetto. Per riavviare il ciclo di elaborazione dell'applicazione, il thread del debugger chiama ilstop
Metodo. Questo metodo imposta unterminate
attributo, che segnala agli altri thread di chiudersi.
Esempio Pacchetti/123456789012-debug_server-1.0/application.py — Metodo Stop
# Interrupt processing loop def stop(self): """Signal application to stop processing.""" logger.info("STOPPING APPLICATION") # Signal processes to stop self.terminate = True # HTTP debug server def run_debugger(self): """Process debug commands from local network.""" class ServerHandler(SimpleHTTPRequestHandler): # Store reference to application application = self # Get status def do_GET(self): """Process GET requests.""" logger.info('Get request to {}'.format(self.path)) if self.path == "/status": self.send_200('OK') else: self.send_error(400) # Restart application def do_POST(self): """Process POST requests.""" logger.info('Post request to {}'.format(self.path)) if self.path == '/restart': self.send_200('OK')
ServerHandler.application.stop()
else: self.send_error(400)