Request a new connection from the tracker. Parameters ---------- key : str The type key of the device. priority : int, optional The priority of the request. session_timeout : float, optional The duration of the session, a
(
self,
key,
priority=1,
session_timeout=0,
max_retry=5,
session_constructor_args=None,
)
| 382 | return res |
| 383 | |
| 384 | def request( |
| 385 | self, |
| 386 | key, |
| 387 | priority=1, |
| 388 | session_timeout=0, |
| 389 | max_retry=5, |
| 390 | session_constructor_args=None, |
| 391 | ): |
| 392 | """Request a new connection from the tracker. |
| 393 | |
| 394 | Parameters |
| 395 | ---------- |
| 396 | key : str |
| 397 | The type key of the device. |
| 398 | |
| 399 | priority : int, optional |
| 400 | The priority of the request. |
| 401 | |
| 402 | session_timeout : float, optional |
| 403 | The duration of the session, allows server to kill |
| 404 | the connection when duration is longer than this value. |
| 405 | When duration is zero, it means the request must always be kept alive. |
| 406 | |
| 407 | max_retry : int, optional |
| 408 | Maximum number of times to retry before give up. |
| 409 | |
| 410 | session_constructor_args : list, optional |
| 411 | List of additional arguments to passed as the remote session constructor. |
| 412 | The first element of the list is always a string specifying the name of |
| 413 | the session constructor, the following args are the positional args to that function. |
| 414 | """ |
| 415 | last_err = None |
| 416 | for _ in range(max_retry): |
| 417 | try: |
| 418 | if self._sock is None: |
| 419 | self._connect() |
| 420 | base.sendjson(self._sock, [base.TrackerCode.REQUEST, key, "", priority]) |
| 421 | value = base.recvjson(self._sock) |
| 422 | if value[0] != base.TrackerCode.SUCCESS: |
| 423 | raise RuntimeError(f"Invalid return value {value!s}") |
| 424 | url, port, matchkey = value[1] |
| 425 | return connect( |
| 426 | url, |
| 427 | port, |
| 428 | matchkey, |
| 429 | session_timeout, |
| 430 | session_constructor_args=session_constructor_args, |
| 431 | ) |
| 432 | except OSError as err: |
| 433 | self.close() |
| 434 | last_err = err |
| 435 | except RuntimeError as err: |
| 436 | last_err = err |
| 437 | raise RuntimeError(f"Cannot request {key} after {max_retry} retry, last_error:{last_err!s}") |
| 438 | |
| 439 | def request_and_run(self, key, func, priority=1, session_timeout=0, max_retry=2): |
| 440 | """Request a resource from tracker and run the func. |
no test coverage detected