Python源码示例:concurrent.futures.CancelledError()
示例1
def _do_heartbeat(self):
while True:
try:
if self._socket.getsockopt(zmq.TYPE) == zmq.ROUTER:
yield from self._do_router_heartbeat()
elif self._socket.getsockopt(zmq.TYPE) == zmq.DEALER:
yield from self._do_dealer_heartbeat()
yield from asyncio.sleep(self._heartbeat_interval,
loop=self._event_loop)
except CancelledError: # pylint: disable=try-except-raise
# The concurrent.futures.CancelledError is caught by asyncio
# when the Task associated with the coroutine is cancelled.
# The raise is required to stop this component.
raise
except Exception as e: # pylint: disable=broad-except
LOGGER.exception(
"An error occurred while sending heartbeat: %s", e)
示例2
def wait(self, timeout: Union[float, datetime.timedelta] = None) -> Awaitable[None]:
"""Block until the internal flag is true.
Returns an awaitable, which raises `tornado.util.TimeoutError` after a
timeout.
"""
fut = Future() # type: Future[None]
if self._value:
fut.set_result(None)
return fut
self._waiters.add(fut)
fut.add_done_callback(lambda fut: self._waiters.remove(fut))
if timeout is None:
return fut
else:
timeout_fut = gen.with_timeout(
timeout, fut, quiet_exceptions=(CancelledError,)
)
# This is a slightly clumsy workaround for the fact that
# gen.with_timeout doesn't cancel its futures. Cancelling
# fut will remove it from the waiters list.
timeout_fut.add_done_callback(
lambda tf: fut.cancel() if not fut.done() else None
)
return timeout_fut
示例3
def test_recv_json_cancelled(self):
@asyncio.coroutine
def test():
a, b = self.create_bound_pair(zmq.PUSH, zmq.PULL)
f = b.recv_json()
assert not f.done()
f.cancel()
# cycle eventloop to allow cancel events to fire
yield from asyncio.sleep(0)
obj = dict(a=5)
yield from a.send_json(obj)
with pytest.raises(CancelledError):
recvd = yield from f
assert f.done()
# give it a chance to incorrectly consume the event
events = yield from b.poll(timeout=5)
assert events
yield from asyncio.sleep(0)
# make sure cancelled recv didn't eat up event
f = b.recv_json()
recvd = yield from asyncio.wait_for(f, timeout=5)
assert recvd == obj
self.loop.run_until_complete(test())
示例4
def run(self):
while not self.closed:
try:
segment, future = self.futures.get(block=True, timeout=0.5)
except queue.Empty:
continue
# End of stream
if future is None:
break
while not self.closed:
try:
result = future.result(timeout=0.5)
except futures.TimeoutError:
continue
except futures.CancelledError:
break
if result is not None:
self.write(segment, result)
break
self.close()
示例5
def wait(self, timeout=None):
"""Block until the internal flag is true.
Returns a Future, which raises `tornado.util.TimeoutError` after a
timeout.
"""
fut = Future()
if self._value:
fut.set_result(None)
return fut
self._waiters.add(fut)
fut.add_done_callback(lambda fut: self._waiters.remove(fut))
if timeout is None:
return fut
else:
timeout_fut = gen.with_timeout(timeout, fut, quiet_exceptions=(CancelledError,))
# This is a slightly clumsy workaround for the fact that
# gen.with_timeout doesn't cancel its futures. Cancelling
# fut will remove it from the waiters list.
timeout_fut.add_done_callback(lambda tf: fut.cancel() if not fut.done() else None)
return timeout_fut
示例6
def wait(self, timeout: Union[float, datetime.timedelta] = None) -> Awaitable[None]:
"""Block until the internal flag is true.
Returns an awaitable, which raises `tornado.util.TimeoutError` after a
timeout.
"""
fut = Future() # type: Future[None]
if self._value:
fut.set_result(None)
return fut
self._waiters.add(fut)
fut.add_done_callback(lambda fut: self._waiters.remove(fut))
if timeout is None:
return fut
else:
timeout_fut = gen.with_timeout(
timeout, fut, quiet_exceptions=(CancelledError,)
)
# This is a slightly clumsy workaround for the fact that
# gen.with_timeout doesn't cancel its futures. Cancelling
# fut will remove it from the waiters list.
timeout_fut.add_done_callback(
lambda tf: fut.cancel() if not fut.done() else None
)
return timeout_fut
示例7
def wait(self, timeout: Union[float, datetime.timedelta] = None) -> Awaitable[None]:
"""Block until the internal flag is true.
Returns an awaitable, which raises `tornado.util.TimeoutError` after a
timeout.
"""
fut = Future() # type: Future[None]
if self._value:
fut.set_result(None)
return fut
self._waiters.add(fut)
fut.add_done_callback(lambda fut: self._waiters.remove(fut))
if timeout is None:
return fut
else:
timeout_fut = gen.with_timeout(
timeout, fut, quiet_exceptions=(CancelledError,)
)
# This is a slightly clumsy workaround for the fact that
# gen.with_timeout doesn't cancel its futures. Cancelling
# fut will remove it from the waiters list.
timeout_fut.add_done_callback(
lambda tf: fut.cancel() if not fut.done() else None
)
return timeout_fut
示例8
def test_server(self):
host = "0.0.0.0"
port = 5489
socket_timeout = 60
def timeout_server():
# need a more robust mechanism for when to cancel the future
time.sleep(2)
self.stop_server_future.cancel()
thread = threading.Thread(target=timeout_server)
thread.daemon = True
thread.start()
with self.assertRaises(CancelledError):
start_server(host=host, port=port,
loop=self.loop,
socket_timeout=socket_timeout,
stop_server_future=self.stop_server_future)
示例9
def start_login(self, request: web.Request) -> web.Response:
user_id = self.verify_token(request)
manual = request.query.get("manual")
if manual and (manual == "1" or manual.lower() == "true"):
manual = True
else:
manual = False
user = u.User.get_by_mxid(user_id)
if user.client:
return web.json_response({
"status": "success",
"name": await user.name_future,
})
try:
return web.json_response(self.ongoing[user.mxid].current_status)
except KeyError:
pass
login = WebCredentialsPrompt(self, user, manual, self.device_name, self.loop)
self.ongoing[user.mxid] = login
try:
return web.json_response(await login.start())
except asyncio.CancelledError:
raise ErrorResponse(410, "Login cancelled", "HANGOUTS_LOGIN_CANCELLED")
示例10
def _receive_credential(self, result_type: CredentialType) -> Optional[str]:
if self.cancelled:
return None
self.current_status = {
"next_step": result_type.value,
}
if result_type == CredentialType.AUTHORIZATION:
self.current_status["manual_auth_url"] = make_login_url(self.device_name)
try:
return self.queue.send_to_async(self.current_status,
lambda: self._set_expecting(result_type))
except futures.TimeoutError:
self.cancel()
return None
except futures.CancelledError:
return None
示例11
def test_recv_json_cancelled(self):
@asyncio.coroutine
def test():
a, b = self.create_bound_pair(zmq.PUSH, zmq.PULL)
f = b.recv_json()
assert not f.done()
f.cancel()
# cycle eventloop to allow cancel events to fire
yield from asyncio.sleep(0)
obj = dict(a=5)
yield from a.send_json(obj)
with pytest.raises(CancelledError):
recvd = yield from f
assert f.done()
# give it a chance to incorrectly consume the event
events = yield from b.poll(timeout=5)
assert events
yield from asyncio.sleep(0)
# make sure cancelled recv didn't eat up event
f = b.recv_json()
recvd = yield from asyncio.wait_for(f, timeout=5)
assert recvd == obj
self.loop.run_until_complete(test())
示例12
def wait(self, timeout=None):
"""Block until the internal flag is true.
Returns a Future, which raises `tornado.util.TimeoutError` after a
timeout.
"""
fut = Future()
if self._value:
fut.set_result(None)
return fut
self._waiters.add(fut)
fut.add_done_callback(lambda fut: self._waiters.remove(fut))
if timeout is None:
return fut
else:
timeout_fut = gen.with_timeout(timeout, fut, quiet_exceptions=(CancelledError,))
# This is a slightly clumsy workaround for the fact that
# gen.with_timeout doesn't cancel its futures. Cancelling
# fut will remove it from the waiters list.
timeout_fut.add_done_callback(lambda tf: fut.cancel() if not fut.done() else None)
return timeout_fut
示例13
def serve_forever(self):
if self._serving_forever_fut is not None:
raise RuntimeError(
f'server {self!r} is already being awaited on serve_forever()')
if self._sockets is None:
raise RuntimeError(f'server {self!r} is closed')
self._start_serving()
self._serving_forever_fut = self._loop.create_future()
try:
await self._serving_forever_fut
except futures.CancelledError:
try:
self.close()
await self.wait_closed()
finally:
raise
finally:
self._serving_forever_fut = None
示例14
def start(self):
try:
if not self.isConnected():
await self._connect()
while self.isConnected():
await self.client.parse()
except CancelledError:
pass
except Exception as err:
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback)
log.info("FICSConnection.run: %s" % repr(err),
extra={"task": (self.host, "raw")})
self.close()
if isinstance(err,
(IOError, LogOnException, EOFError, socket.error,
socket.gaierror, socket.herror)):
self.emit("error", err)
else:
raise
finally:
if isinstance(self, FICSMainConnection):
self.emit("disconnected")
示例15
def submit(self, label, command, opts):
"""Submit a bgutil command to run it asynchronously."""
task = BgUtilTask(label, datetime.now())
self.tasks.append(task)
def _done_callback(f):
try:
result = f.result()
task.completed(result)
except futures.CancelledError as e:
task.cancelled(e)
except futures.TimeoutError as e:
task.timed_out(e)
except Exception as e:
task.failed(e)
future = self._executor.submit(
self._wrap_command, task, context.accessor, command, opts
)
task.submitted()
future.add_done_callback(_done_callback)
示例16
def serve_forever(self):
if self._serving_forever_fut is not None:
raise RuntimeError(
f'server {self!r} is already being awaited on serve_forever()')
if self._sockets is None:
raise RuntimeError(f'server {self!r} is closed')
self._start_serving()
self._serving_forever_fut = self._loop.create_future()
try:
await self._serving_forever_fut
except futures.CancelledError:
try:
self.close()
await self.wait_closed()
finally:
raise
finally:
self._serving_forever_fut = None
示例17
def wait(self, _timeout=None):
if self._awaited:
try:
self._future.result(timeout=_timeout)
except FutureTimeoutError:
self.set_timedout()
except FutureCancelledError:
self.cancel()
return self
示例18
def test_CancellableFuture_can_be_cancelled_while_it_is_running(observer_runner):
from concurrent.futures import ThreadPoolExecutor, CancelledError
from moler.runner import CancellableFuture
# concurrent.futures.Future can't cancel() while it is already running
stop_running = threading.Event()
is_done = threading.Event()
def activity(stop_running, is_done):
while not stop_running.is_set():
time.sleep(0.5)
is_done.set()
future = ThreadPoolExecutor().submit(activity, stop_running, is_done)
observer_lock = threading.Lock()
c_future = CancellableFuture(future, observer_lock, stop_running, is_done)
try:
time.sleep(0.1) # allow threads switch to ensure future running
assert c_future.running()
cancelled = c_future.cancel()
time.sleep(0.1) # allow threads switch
assert not c_future.running()
assert is_done.is_set()
assert cancelled is True
assert c_future.cancelled()
assert c_future.done()
with pytest.raises(CancelledError):
c_future.result()
except AssertionError:
raise
finally:
stop_running.set()
# --------------------------- resources ---------------------------
示例19
def run_tasks(self):
""" Run the tasks attached to the instance """
tasks = self.get_tasks()
self._gathered_tasks = asyncio.gather(*tasks, loop=self.loop)
try:
await self._gathered_tasks
except CancelledError:
pass
示例20
def _remove_expired_futures(self):
while True:
try:
yield from asyncio.sleep(self._connection_timeout,
loop=self._event_loop)
self._futures.remove_expired()
except CancelledError: # pylint: disable=try-except-raise
# The concurrent.futures.CancelledError is caught by asyncio
# when the Task associated with the coroutine is cancelled.
# The raise is required to stop this component.
raise
except Exception as e: # pylint: disable=broad-except
LOGGER.exception("An error occurred while"
" cleaning up expired futures: %s",
e)
示例21
def _dispatch_message(self):
while True:
try:
zmq_identity, msg_bytes = \
yield from self._dispatcher_queue.get()
self._get_queue_size_gauge(self.connection).set_value(
self._dispatcher_queue.qsize())
message = validator_pb2.Message()
message.ParseFromString(msg_bytes)
tag = get_enum_name(message.message_type)
self._get_received_message_counter(tag).inc()
if zmq_identity is not None:
connection_id = \
self._identity_to_connection_id(zmq_identity)
else:
connection_id = \
self._identity_to_connection_id(
self._connection.encode())
try:
self._futures.set_result(
message.correlation_id,
future.FutureResult(
message_type=message.message_type,
content=message.content,
connection_id=connection_id))
except future.FutureCollectionKeyError:
self._dispatcher.dispatch(self._connection,
message,
connection_id)
except CancelledError: # pylint: disable=try-except-raise
# The concurrent.futures.CancelledError is caught by asyncio
# when the Task associated with the coroutine is cancelled.
# The raise is required to stop this component.
raise
except Exception as e: # pylint: disable=broad-except
LOGGER.exception("Received a message on address %s that "
"caused an error: %s", self._address, e)
示例22
def _receive_message(self):
"""
Internal coroutine for receiving messages
"""
while True:
try:
if self._socket.getsockopt(zmq.TYPE) == zmq.ROUTER:
zmq_identity, msg_bytes = \
yield from self._socket.recv_multipart()
if msg_bytes == b'':
# send ACK for connection probes
LOGGER.debug("ROUTER PROBE FROM %s", zmq_identity)
self._socket.send_multipart(
[bytes(zmq_identity), msg_bytes])
else:
self._received_from_identity(zmq_identity)
self._dispatcher_queue.put_nowait(
(zmq_identity, msg_bytes))
else:
msg_bytes = yield from self._socket.recv()
self._last_message_time = time.time()
self._dispatcher_queue.put_nowait((None, msg_bytes))
self._get_queue_size_gauge(self.connection).set_value(
self._dispatcher_queue.qsize())
except CancelledError: # pylint: disable=try-except-raise
# The concurrent.futures.CancelledError is caught by asyncio
# when the Task associated with the coroutine is cancelled.
# The raise is required to stop this component.
raise
except Exception as e: # pylint: disable=broad-except
LOGGER.exception("Received a message on address %s that "
"caused an error: %s", self._address, e)
示例23
def _monitor_disconnects(self):
while True:
try:
yield from self._monitor_sock.recv_multipart()
self._check_connections()
except CancelledError: # pylint: disable=try-except-raise
# The concurrent.futures.CancelledError is caught by asyncio
# when the Task associated with the coroutine is cancelled.
# The raise is required to stop this component.
raise
except Exception as e: # pylint: disable=broad-except
LOGGER.exception(
"An error occurred while sending heartbeat: %s", e)
示例24
def update_stats_on_done(stats, fut):
# This utilizes the fact that python, non-primitive types are
# passed by reference (not by value)...
stats['failover_attempted'] += 1
try:
fut.result()
except futures.CancelledError:
stats['failover_cancelled'] += 1
except Exception:
stats['failover_failed'] += 1
示例25
def handle_exception(loop, context):
exc = context.get('exception')
if exc is None or isinstance(exc, CancelledError):
return # not an error, cleanup message
if isinstance(exc, ExitMainLoop):
Shutdown.set() # use previously stored exit code
return
if Error.is_set():
return # already reporting an error
Error.set()
exc_info = (type(exc), exc, exc.__traceback__)
if any(pred(exc) for pred in NOTRACK_EXCEPTIONS):
app.log.debug('Would not track exception: {}'.format(exc))
if not (app.no_report or any(pred(exc) for pred in NOTRACK_EXCEPTIONS)):
track_exception(str(exc))
utils.sentry_report(exc_info=exc_info)
msg = 'Unhandled exception'
if 'future' in context:
msg += ' in {}'.format(context['future'])
app.log.exception(msg, exc_info=exc)
if app.headless:
msg = str(exc)
utils.error(msg)
Shutdown.set(1)
else:
app.exit_code = 1 # store exit code for later
app.ui.show_exception_message(exc) # eventually raises ExitMainLoop
示例26
def shutdown_watcher():
app.log.info('Watching for shutdown')
try:
try:
await Shutdown.wait()
except asyncio.CancelledError:
pass
app.log.info('Shutting down')
if app.headless:
utils.warning('Shutting down')
# Store application configuration state
await app.save()
if app.juju.authenticated:
app.log.info('Disconnecting model')
await app.juju.client.disconnect()
app.log.info('Disconnected')
if not app.headless:
EventLoop.remove_alarms()
for task in asyncio.Task.all_tasks(app.loop):
# cancel all other tasks
coro = getattr(task, '_coro', None)
cr_code = getattr(coro, 'cr_code', None)
if cr_code is not shutdown_watcher.__code__:
app.log.debug('Cancelling pending task: {}'.format(task))
task.cancel()
await asyncio.sleep(0.1) # give tasks a chance to see the cancel
except Exception as e:
app.log.exception('Error in cleanup code: {}'.format(e))
app.loop.stop()
示例27
def test_cancel(ActorClass):
print('-----------------')
print('Test cancel for {}'.format(ActorClass))
test_state = {'num': False}
def done_callback(f):
try:
num = f.result()
except futures.CancelledError:
num = 'canceled'
print('Canceled task {}'.format(f))
else:
test_state['num'] += num
print('DONE CALLBACK GOT = {}'.format(num))
executor = ActorClass.executor()
f1 = executor.post({'action': 'wait', 'time': 1})
f1.add_done_callback(done_callback)
f2 = executor.post({'action': 'wait', 'time': 2})
f2.add_done_callback(done_callback)
f3 = executor.post({'action': 'wait', 'time': 3})
f3.add_done_callback(done_callback)
f4 = executor.post({'action': 'wait', 'time': 4})
f4.add_done_callback(done_callback)
can_cancel = f3.cancel()
# print('can_cancel = %r' % (can_cancel,))
assert can_cancel, 'we should be able to cancel in time'
f4.result()
assert test_state['num'] == 7, 'f3 was not cancelled'
print('Test completed')
print('L______________')
示例28
def _kill_subprocess(self, proc: Optional[Process]) -> None:
"""Helper method; send SIGTERM/SIGKILL to a subprocess.
This method first sends SIGTERM to the subprocess. If the process hasn't terminated
after a given timeout, it sends SIGKILL.
Parameter
---------
proc : Optional[Process]
the process to attempt to terminate. If None, this method does nothing.
"""
if proc is not None:
if proc.returncode is None:
try:
proc.terminate()
try:
await asyncio.shield(asyncio.wait_for(proc.wait(), self._cancel_timeout))
except CancelledError:
pass
if proc.returncode is None:
proc.kill()
try:
await asyncio.shield(
asyncio.wait_for(proc.wait(), self._cancel_timeout))
except CancelledError:
pass
except ProcessLookupError:
pass
示例29
def _wait_for_direct_ACK(self):
_LOGGER.debug("Starting Device._wait_for_direct_ACK")
msg = None
while True:
# wait for an item from the queue
try:
with async_timeout.timeout(DIRECT_ACK_WAIT_TIMEOUT):
msg = await self._directACK_received_queue.get()
_LOGGER.debug(
"Remaining queue %d, Direct ACK: %s:%s",
self._directACK_received_queue.qsize(),
id(msg),
msg,
)
break
except asyncio.TimeoutError:
_LOGGER.debug("No direct ACK messages received.")
break
except CancelledError:
break
# _LOGGER.debug('Holding lock for 10 seconds')
# await asyncio.sleep(10, loop=self._plm.loop)
_LOGGER.debug("Releasing lock after processing direct ACK")
if self._send_msg_lock.locked():
self._send_msg_lock.release()
_LOGGER.debug("Device %s msg_lock unlocked", self._address.human)
if msg or self._sent_msg_wait_for_directACK.get("on_timeout"):
callback = self._sent_msg_wait_for_directACK.get("callback", None)
if callback is not None:
_LOGGER.debug("Scheduling msg directACK callback: %s", callback)
callback(msg)
self._sent_msg_wait_for_directACK = {}
_LOGGER.debug("Ending Device._wait_for_direct_ACK")
示例30
def _receiver(self):
try:
while self.is_open:
result = await utils.run_with_interrupt(
self.ws.recv(),
self.monitor.close_called,
loop=self.loop)
if self.monitor.close_called.is_set():
break
if result is not None:
result = json.loads(result)
await self.messages.put(result['request-id'], result)
except CancelledError:
pass
except websockets.ConnectionClosed as e:
log.warning('Receiver: Connection closed, reconnecting')
await self.messages.put_all(e)
# the reconnect has to be done as a task because the receiver will
# be cancelled by the reconnect and we don't want the reconnect
# to be aborted half-way through
self.loop.create_task(self.reconnect())
return
except Exception as e:
log.exception("Error in receiver")
# make pending listeners aware of the error
await self.messages.put_all(e)
raise