Spaces:
Runtime error
Runtime error
Commit
·
dcf7e45
1
Parent(s):
58754bb
Add MonsterAPI Client timeout
Browse files- MonsterAPIClient.py +9 -3
MonsterAPIClient.py
CHANGED
@@ -123,8 +123,14 @@ class MClient():
|
|
123 |
response.raise_for_status()
|
124 |
return response.json()
|
125 |
|
126 |
-
def wait_and_get_result(self, process_id):
|
|
|
127 |
while True:
|
|
|
|
|
|
|
|
|
|
|
128 |
status = self.get_status(process_id)
|
129 |
if status['status'].lower() == 'completed':
|
130 |
return status['result']
|
@@ -132,9 +138,9 @@ class MClient():
|
|
132 |
raise RuntimeError(f"Process {process_id} failed!")
|
133 |
else:
|
134 |
if self.mock:
|
135 |
-
return 100*"Mock Output!"
|
136 |
logger.info(f"Process {process_id} is still running, status is {status['status']}. Waiting for 5 seconds...")
|
137 |
-
time.sleep(
|
138 |
|
139 |
|
140 |
if __name__ == '__main__':
|
|
|
123 |
response.raise_for_status()
|
124 |
return response.json()
|
125 |
|
126 |
+
def wait_and_get_result(self, process_id, timeout=300):
|
127 |
+
start_time = time.time()
|
128 |
while True:
|
129 |
+
elapsed_time = time.time() - start_time
|
130 |
+
|
131 |
+
if elapsed_time >= timeout:
|
132 |
+
raise TimeoutError(f"Process {process_id} timed out after {timeout} seconds.")
|
133 |
+
|
134 |
status = self.get_status(process_id)
|
135 |
if status['status'].lower() == 'completed':
|
136 |
return status['result']
|
|
|
138 |
raise RuntimeError(f"Process {process_id} failed!")
|
139 |
else:
|
140 |
if self.mock:
|
141 |
+
return 100 * "Mock Output!"
|
142 |
logger.info(f"Process {process_id} is still running, status is {status['status']}. Waiting for 5 seconds...")
|
143 |
+
time.sleep(0.05)
|
144 |
|
145 |
|
146 |
if __name__ == '__main__':
|