def spawnThreads(self, indexes=[0, 1, 2, 3]): '''Spawns a new thread for each plotting command selected by the user. ''' for index in indexes: if self.savedPlotCommands[index]: #sleep between spawning threads or else the device may be overloaded with requests time.sleep(.5) print 'spawning thread ', index thread = Thread.Thread(self.deviceHandler, self.savedPlotCommands[index], index) thread.start() #store a reference so the thread isn't garbage collected. self.plottingThreads.append(thread)
def get_mean_color_multi(image): args = dict() args["image"] = image number_of_threads = 4 mythread = Thread(number_of_threads, get_mean_color_multi_thread, args) mythread.start() mean = mythread.get_data() mythread.join() blue = red = green = 0 for i in mean: blue += i[0] green += i[1] red += i[2] blue = int(blue / number_of_threads) green = int(green / number_of_threads) red = int(red / number_of_threads) return (blue, green, red)
#!/usr/bin/env python3 from hwalert import hwalert from keypad import check_input from utils import Thread hwalert.on('Test') input_thread = Thread(check_input)
def start(self): self.thread = Thread(self.check_keypad_input)
def get_results(): try: result_form = ResultForm() reset_form = ResetButtons() plot_url = None code = request.args.get('code', '') if request.method == 'POST': if reset_form.submit.data: return redirect(url_for('home')) elif reset_form.reset_all.data: shutil.rmtree(results_dir) shutil.rmtree(model_path_dir) shutil.rmtree(test_path_dir) shutil.rmtree(train_path_dir) os.makedirs(results_dir) os.makedirs(model_path_dir) os.makedirs(test_path_dir) os.makedirs(train_path_dir) return redirect(url_for('home')) elif result_form.download_plot.data: if GlobalFlags.is_recently_tested: return send_file(results_plot_test_path, mimetype='image/png', as_attachment=True) else: return send_file(results_plot_train_path, mimetype='image/png', as_attachment=True) elif result_form.download_pred.data: return send_file(results_pred_path, mimetype='text/csv', as_attachment=True) elif result_form.download_info.data: return send_file(results_info_path, mimetype='application/json', as_attachment=True) elif result_form.download_test.data: return send_file(results_test_path, mimetype='text/csv', as_attachment=True) elif result_form.download_train.data: return send_file(results_train_path, mimetype='text/csv', as_attachment=True) if ((code == '0' or code == '1') and GlobalFlags.is_recently_trained) or \ (code == '2' and GlobalFlags.is_recently_tested): train_loss, test_loss, full_info = get_losses_info(results_train_path, results_test_path, results_info_path) with open(model_path, 'r', encoding='utf-8') as model_file: model_json = json.load(model_file) full_info.update(model_json) if GlobalFlags.is_recently_trained: best_iter, best_train_loss = get_best_by_train(train_loss) result_form.train_loss.data = str(best_train_loss) full_info['best_train_by_train'] = best_train_loss full_info['best_iter_by_train'] = int(best_iter + 1) full_info.pop('best_test_loss_by_test', None) full_info.pop('best_train_loss_by_test', None) full_info.pop('best_iter_by_test', None) plot_url = get_plot(train_loss, [], results_plot_train_path) if GlobalFlags.is_recently_tested: best_iter, best_test_loss, best_train_loss = get_best_by_test(train_loss, test_loss) result_form.train_loss.data = str(best_train_loss) result_form.test_loss.data = str(best_test_loss) full_info['best_test_loss_by_test'] = best_test_loss full_info['best_train_loss_by_test'] = best_train_loss full_info['best_iter_by_test'] = int(best_iter + 1) plot_url = get_plot(train_loss, test_loss, results_plot_test_path) with open(results_info_path, 'w') as info_file: json.dump(full_info, info_file) elif not GlobalFlags.no_f5: if code == '1': GlobalFlags.no_f5 = True thread = Thread() thread.train(data_paths['train'], model_path, model_dumped_path, results_dir, results_train_path, results_info_path, GlobalFlags) elif code == '2': GlobalFlags.no_f5 = True thread = Thread() thread.test(data_paths['test'], model_dumped_path, results_dir, results_test_path, results_pred_path, results_info_path, GlobalFlags) return render_template('results.html', result_form=result_form, reset_form=reset_form, global_flags=GlobalFlags, plot_url=plot_url) except Exception as exc: app.logger.info('Exception: {0}'.format(exc))
def run_for(self, msg, time=0): self.thread = Thread(self._run_for, msg, time)