Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_ask_can_timeout_if_blocked_too_long(actor_ref):
with pytest.raises(Timeout):
actor_ref.ask({'command': 'ping'}, timeout=0)
def test_filter_preserves_the_timeout_kwarg(future):
filtered = future.filter(lambda x: x > 10)
with pytest.raises(Timeout):
filtered.get(timeout=0)
import logging
import time
import pykka
logger = logging.getLogger(__name__)
class Future(pykka.ThreadingFuture):
Timeout = pykka.Timeout
@classmethod
def exception(cls, exc=None):
future = cls()
future.set_exception((type(exc), exc, None))
return future
@classmethod
def fromdbus(cls, func, *args, **kwargs):
method = getattr(func, "_method_name", "")
logger.debug("Calling D-Bus method %s%s", method, args)
future = cls()
start = time.time()
def reply(*args):
logger.debug("%s reply after %.3fs", method, time.time() - start)
def __actor_halt(self, name):
actor = self.get_actor(name)
try:
# We have seen situation where this creates a deadlock. We add a timeout so that we
# eventually return from the get() and force the halt transition.
if not actor.is_halt().get(timeout=1):
actor.halt.defer()
except pykka.Timeout:
actor.halt.defer()
try:
latch = pykka.ThreadingFuture()
proxy.tell(
{
'request': 'execute',
'latch': latch,
'function': func
})
Event()
out = latch.get(timeout=timeout)
if isinstance(out, Exception):
raise out
return out
except Timeout:
assert 0, 'request timeout'
translator.to_mopidy_artist(a) for a in results.artists()],
tracks=[
translator.to_mopidy_track(t) for t in results.tracks()])
future.set(search_result)
if not self.backend.spotify.connected.is_set():
logger.debug('Not connected: Spotify search cancelled')
return SearchResult(uri='spotify:search')
self.backend.spotify.session.search(
spotify_query, callback,
album_count=200, artist_count=200, track_count=200)
try:
return future.get(timeout=self._timeout)
except pykka.Timeout:
logger.debug(
'Timeout: Spotify search did not return in %ds', self._timeout)
return SearchResult(uri='spotify:search')
# - if the termination trigger is set, abort immediately
#
if self.force_reset or self.terminate:
raise Aborted('resetting')
#
# - spin-lock on the controller latch
# - any catastrophic plug failure will be trapped that way
#
try:
Event()
out = data.latch.get(SAMPLING)
if isinstance(out, Exception):
raise out
except Timeout:
pass
return 'lock', data, 0
:type actor_ref: :class:`pykka.ActorRef`
:param actor_ref: a pykka actor reference
:type timeout: float
:param timeout: optional timeout in seconds
"""
try:
if not actor_ref:
return
latch = ThreadingFuture()
actor_ref.tell({'request': 'shutdown', 'latch': latch})
Event()
latch.get(timeout=timeout)
except Timeout:
pass
except ActorDeadError:
pass
def get(self, timeout=None):
try:
return super(ThreadingFuture, self).get(timeout=timeout)
except NotImplementedError:
pass
try:
if self._data is None:
self._data = self._queue.get(True, timeout)
if 'exc_info' in self._data:
_compat.reraise(*self._data['exc_info'])
else:
return self._data['value']
except _compat.queue.Empty:
raise Timeout('{} seconds'.format(timeout))
def stop(self):
if self.bookmarks:
logger.debug('Stopping %s', self.bookmarks)
try:
self.bookmarks.stop(timeout=1)
except pykka.Timeout:
# bookmarks actor may be waiting on backend
pykka.ActorRegistry.unregister(self.bookmarks)