|
18 | 18 | import io
|
19 | 19 | import multiprocessing
|
20 | 20 | import pickle
|
| 21 | +import queue |
21 | 22 | from typing import Any, Callable, Dict, Optional, Union
|
22 | 23 |
|
23 | 24 | from pyglove.core import utils
|
@@ -197,28 +198,30 @@ def _run():
|
197 | 198 | q.put(e)
|
198 | 199 |
|
199 | 200 | q = multiprocessing.Queue()
|
| 201 | + p = multiprocessing.Process( |
| 202 | + target=_call, args=tuple([q] + list(args)), kwargs=kwargs |
| 203 | + ) |
200 | 204 | try:
|
201 |
| - p = multiprocessing.Process( |
202 |
| - target=_call, args=tuple([q] + list(args)), kwargs=kwargs) |
203 | 205 | p.start()
|
204 |
| - p.join(timeout=timeout) |
| 206 | + x = q.get(timeout=timeout) |
| 207 | + except queue.Empty as e: |
205 | 208 | if p.is_alive():
|
206 | 209 | # We use `kill` instead of `terminate` to release process resources
|
207 | 210 | # right away.
|
208 | 211 | p.kill()
|
209 |
| - raise TimeoutError(f'Execution time exceed {timeout} seconds.') |
210 |
| - x = q.get() |
211 |
| - if isinstance(x, Exception): |
212 |
| - raise x |
213 |
| - try: |
214 |
| - return pickle.loads(x) |
215 |
| - except Exception as e: |
216 |
| - raise errors.SerializationError( |
217 |
| - 'Cannot deserialize the output from sandbox.', e |
218 |
| - ) from e |
| 212 | + raise TimeoutError(f'Execution time exceed {timeout} seconds.') from e |
219 | 213 | finally:
|
220 | 214 | q.close()
|
221 | 215 |
|
| 216 | + if isinstance(x, Exception): |
| 217 | + raise x |
| 218 | + try: |
| 219 | + return pickle.loads(x) |
| 220 | + except Exception as e: |
| 221 | + raise errors.SerializationError( |
| 222 | + 'Cannot deserialize the output from sandbox.', e |
| 223 | + ) from e |
| 224 | + |
222 | 225 |
|
223 | 226 | def maybe_sandbox_call(
|
224 | 227 | func: Callable[..., Any],
|
|
0 commit comments