From a48f2f735cfa45a6a998cc08ca05b40d5daed95e Mon Sep 17 00:00:00 2001 From: Florian Maurer Date: Mon, 25 Nov 2024 11:54:52 +0100 Subject: [PATCH] only lock during creation of pandas dataframe in output role --- assume/common/outputs.py | 49 ++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/assume/common/outputs.py b/assume/common/outputs.py index 3dd08ac3..465e2ee8 100644 --- a/assume/common/outputs.py +++ b/assume/common/outputs.py @@ -294,40 +294,39 @@ async def store_dfs(self): return for table in self.write_dfs.keys(): - with self.locks[table]: - if len(self.write_dfs[table]) == 0: + if len(self.write_dfs[table]) == 0: continue - + with self.locks[table]: # concat all dataframes # use join='outer' to keep all columns and fill missing values with NaN df = pd.concat(self.write_dfs[table], axis=0, join="outer") + self.write_dfs[table] = [] - df.reset_index() - if df.empty: - continue + df.reset_index() + if df.empty: + continue - df = df.apply(check_for_tensors) + df = df.apply(check_for_tensors) - if self.export_csv_path: - data_path = self.export_csv_path / f"{table}.csv" - df.to_csv( - data_path, - mode="a", - header=not data_path.exists(), - float_format="%.5g", - ) + if self.export_csv_path: + data_path = self.export_csv_path / f"{table}.csv" + df.to_csv( + data_path, + mode="a", + header=not data_path.exists(), + float_format="%.5g", + ) - if self.db is not None: - try: - with self.db.begin() as db: - df.to_sql(table, db, if_exists="append") - except (ProgrammingError, OperationalError, DataError): - self.check_columns(table, df) - # now try again - with self.db.begin() as db: - df.to_sql(table, db, if_exists="append") + if self.db is not None: + try: + with self.db.begin() as db: + df.to_sql(table, db, if_exists="append") + except (ProgrammingError, OperationalError, DataError): + self.check_columns(table, df) + # now try again + with self.db.begin() as db: + df.to_sql(table, db, if_exists="append") - self.write_dfs[table] = [] self.current_dfs_size = 0