|
4 | 4 | import json |
5 | 5 | from threading import Lock |
6 | 6 | import warnings |
| 7 | +from sqlalchemy.exc import IntegrityError |
7 | 8 |
|
8 | 9 | from sacred.commandline_options import cli_option |
9 | 10 | from sacred.observers.base import RunObserver |
@@ -76,26 +77,34 @@ def _add_event( |
76 | 77 | ): |
77 | 78 | from .sql_bases import Base, Experiment, Host, Run |
78 | 79 |
|
79 | | - Base.metadata.create_all(self.engine) |
80 | | - sql_exp = Experiment.get_or_create(ex_info, self.session) |
81 | | - sql_host = Host.get_or_create(host_info, self.session) |
82 | | - if _id is None: |
83 | | - i = self.session.query(Run).order_by(Run.id.desc()).first() |
84 | | - _id = 0 if i is None else i.id + 1 |
85 | | - |
86 | | - self.run = Run( |
87 | | - run_id=str(_id), |
88 | | - config=json.dumps(flatten(config)), |
89 | | - command=command, |
90 | | - priority=meta_info.get("priority", 0), |
91 | | - comment=meta_info.get("comment", ""), |
92 | | - experiment=sql_exp, |
93 | | - host=sql_host, |
94 | | - status=status, |
95 | | - **kwargs, |
96 | | - ) |
97 | | - self.session.add(self.run) |
98 | | - self.save() |
| 80 | + while True: |
| 81 | + Base.metadata.create_all(self.engine) |
| 82 | + sql_exp = Experiment.get_or_create(ex_info, self.session) |
| 83 | + sql_host = Host.get_or_create(host_info, self.session) |
| 84 | + if _id is None: |
| 85 | + i = self.session.query(Run).order_by(Run.id.desc()).first() |
| 86 | + _id = 0 if i is None else i.id + 1 |
| 87 | + |
| 88 | + self.run = Run( |
| 89 | + run_id=str(_id), |
| 90 | + config=json.dumps(flatten(config)), |
| 91 | + command=command, |
| 92 | + priority=meta_info.get("priority", 0), |
| 93 | + comment=meta_info.get("comment", ""), |
| 94 | + experiment=sql_exp, |
| 95 | + host=sql_host, |
| 96 | + status=status, |
| 97 | + **kwargs, |
| 98 | + ) |
| 99 | + self.session.add(self.run) |
| 100 | + |
| 101 | + with self.lock: |
| 102 | + try: |
| 103 | + self.session.commit() |
| 104 | + except IntegrityError: |
| 105 | + self.session.rollback() |
| 106 | + else: |
| 107 | + break |
99 | 108 | return _id or self.run.run_id |
100 | 109 |
|
101 | 110 | def heartbeat_event(self, info, captured_out, beat_time, result): |
|
0 commit comments