def check_notebook_name_exists(check_notebook_name_exists_json): check_notebook_name_exists_dict = json_decoder.decode( check_notebook_name_exists_json) # if new installation, create global notebooks table if not (notebook_global_table_exist()): create_notebook_global_table() # opens the global notebooks table, and checks whether notebook exists fileObject = open("NOTEBOOKS_DATA", "rb") table = pickle.load(fileObject) if any(obj['notebook_name'] == check_notebook_name_exists_dict['notebook_name'] for obj in table): return json_encoder.encode({ "message": "Success", "comment": "Notebook name exists" }) return json_encoder.encode({ "message": "Success", "comment": "Notebook name available" })
def add_notebook(): print("changed") try: # notebook is of type weakdict to create references of it notebook = weakdict(request.json) # creating a reference of notebook's data weakdict_notebook = weakref.proxy(notebook) fileObject = open("NOTEBOOK_" + notebook['notebook_name'], "wb") pickle.dump(notebook, fileObject) fileObject.close() # if new installation, create a global notebooks table if not (notebook_global_table_exist()): create_notebook_global_table() # open global notebooks table and add notebook configuration fileObject = open("NOTEBOOKS_DATA", "rb") table = pickle.load(fileObject) fileObject.close() table.append({ "notebook_name": notebook['notebook_name'], "GPU_count": int(notebook['GPU_count']), "CPU_count": int(notebook['CPU_count']), "is_online": False }) fileObject = open("NOTEBOOKS_DATA", "wb") pickle.dump(table, fileObject) fileObject.close() # open global user table and add this notebook to user's list of created notebooks table = pickle.load(open("USERTABLE", "rb")) for obj in table: if obj['username'] == notebook['username']: obj['created_notebooks'].append(notebook['notebook_name']) pickle.dump(table, open("USERTABLE", "wb")) return json_encoder.encode({ "message": "Success", "comment": "Notebook created" }) except: return json_encoder.encode({ "message": "Failure", "comment": "Other error" })
def get_devices(): # Currently works only on Linux # Fails on windows. # Check for unix if (os.name == 'posix'): n_cpu = psutil.cpu_count() # if new installation, create a global notebooks table if not (notebook_global_table_exist()): create_notebook_global_table() table = pickle.load(open("NOTEBOOKS_DATA", "rb")) a_cpu = n_cpu - sum(obj["CPU_count"] for obj in table) return json_encoder.encode({"message": "Success", "CPU_available": a_cpu})
def get_devices(): # Currently works only on Linux # Fails on windows. # Check for unix if (os.name == 'posix'): # get GPU count if avaiable try: n_gpu = len(GPUtil.getGPUs()) except: n_gpu = 0 # get CPU count n_cpu = psutil.cpu_count() # For windows, use tensorflow to get details else: local_devices = device_lib.list_local_devices() n_gpu = len([x.name for x in local_devices if x.device_type == 'GPU']) n_cpu = psutil.cpu_count() # if new installation, create a global notebooks table if not (notebook_global_table_exist()): create_notebook_global_table() table = pickle.load(open("NOTEBOOKS_DATA", "rb")) # Send number of CPUs and GPUs currently available a_gpu = n_gpu - sum(obj["GPU_count"] for obj in table if obj['is_online']) a_cpu = n_cpu - sum(obj["CPU_count"] for obj in table if obj['is_online']) return json_encoder.encode({ "message": "Success", "GPU_count": n_gpu, "CPU_count": n_cpu, "GPU_available": a_gpu, "CPU_available": a_cpu })