本文整理汇总了Python中asyncio.Future.set_exception方法的典型用法代码示例。如果您正苦于以下问题:Python Future.set_exception方法的具体用法?Python Future.set_exception怎么用?Python Future.set_exception使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asyncio.Future
的用法示例。
在下文中一共展示了Future.set_exception方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: filter_stderr
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
def filter_stderr(popen:subprocess.Popen, future:asyncio.Future):
last_ex = None
while True:
data = popen.stderr.readline()
if data:
# print("FFmpeg says:", data, flush=True)
try:
if check_stderr(data):
sys.stderr.buffer.write(data)
sys.stderr.buffer.flush()
except FFmpegError as e:
print("Error from FFmpeg:", e)
last_ex = e
except FFmpegWarning:
pass # useless message
else:
break
if last_ex:
future.set_exception(last_ex)
else:
future.set_result(True)
示例2: filter_stderr
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
def filter_stderr(popen:subprocess.Popen, future:asyncio.Future):
last_ex = None
while True:
data = popen.stderr.readline()
if data:
log.ffmpeg("Data from ffmpeg: {}".format(data))
try:
if check_stderr(data):
sys.stderr.buffer.write(data)
sys.stderr.buffer.flush()
except FFmpegError as e:
log.ffmpeg("Error from ffmpeg: %s", str(e).strip())
last_ex = e
except FFmpegWarning:
pass # useless message
else:
break
if last_ex:
future.set_exception(last_ex)
else:
future.set_result(True)
示例3: as_future
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
def as_future(fun, *args, **kwargs):
"""
Executes a function with the given arguments
and wraps the result into a future.
:param fun: The function to be called.
:type fun: func
:param args: The argument list for the supplied function.
:type args: list
:param kwargs: The keyword argument dict for the supplied function.
:type kwargs: dict
:return: The functions result wrapped in a Future
:rtype: asyncio.Future
"""
try:
res = fun(*args, **kwargs)
except Exception as e:
f = Future()
f.set_exception(e)
return f
else:
if isinstance(res, Future):
return res
elif iscoroutine(res):
return asyncio.Task(res)
else:
f = Future()
f.set_result(res)
return f
示例4: _as_future
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
def _as_future(fun, *args, **kwargs):
try:
res = fun(*args, **kwargs)
except Exception as e:
f = Future()
f.set_exception(e)
return f
else:
if isinstance(res, Future):
return res
elif iscoroutine(res):
return asyncio.Task(res)
else:
f = Future()
f.set_result(res)
return f
示例5: _send_request
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
def _send_request(self, app_name, endpoint, entity, params):
packet = MessagePacket.request(self.name, self.version, app_name, _Service._REQ_PKT_STR, endpoint, params,
entity)
future = Future()
request_id = params['request_id']
self._pending_requests[request_id] = future
try:
self.tcp_bus.send(packet)
except ClientException as e:
if not future.done() and not future.cancelled():
ERROR = '101_Client not found'
exception = RequestException(ERROR)
exception.error = ERROR
future.set_exception(exception)
_Service.time_future(future, TCPServiceClient.REQUEST_TIMEOUT_SECS)
return future
示例6: _send_request
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
def _send_request(self, app_name, endpoint, entity, params, timeout):
packet = MessagePacket.request(self.name, self.version, app_name, _Service._REQ_PKT_STR, endpoint, params,
entity)
future = Future()
request_id = params['request_id']
self._pending_requests[request_id] = future
try:
self.tcp_bus.send(packet)
except ClientException:
if not future.done() and not future.cancelled():
error = 'Client not found'
exception = ClientException(error)
exception.error = error
future.set_exception(exception)
_Service.time_future(future, timeout)
return future
示例7: _uwsgi_entry_point
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
async def _uwsgi_entry_point(self, current_greenlet: greenlet.greenlet, future: asyncio.Future,
*args, **kwargs):
"""
uWSGI wrapper entry point.
"""
try:
# Call the underlying wrapped function.
result = await func(self, *args, **kwargs)
except Exception as e:
# Set the exception on the Future, allowing the web server to retrieve it.
future.set_exception(e)
else:
# Set the result of the function on the Future.
future.set_result(result)
finally:
# Switch back context.
current_greenlet.switch()
示例8: go
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
def go():
error = Exception('woops')
future = Future()
future.set_exception(error)
source = Observable.from_future(future)
def on_next(x):
success[0] = False
def on_error(err):
success[1] = str(err) == str(error)
def on_completed():
success[2] = False
subscription = source.subscribe(on_next, on_error, on_completed)
示例9: test_sends_ping
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
async def test_sends_ping(make_transport, make_fut):
transp = make_transport()
future = Future()
future.set_result(False)
ws = mock.Mock()
ws.ping.side_effect = [future]
hb_future = Future()
hb_future.set_result((FRAME_HEARTBEAT, b""))
session_close_future = Future()
session_close_future.set_exception(SessionIsClosed)
session = mock.Mock()
session._wait.side_effect = [hb_future, session_close_future]
await transp.server(ws, session)
assert ws.ping.called
示例10: _launcher
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
async def _launcher(self, coro, callback_future: asyncio.Future = None):
exception = None
try:
ret = await coro
except Exception as e:
exception = e
pass
finally:
self._current_workers -= 1
while self._current_workers < self._max_workers:
waiting_future = None
while len(self._pending_queue) > 0:
generator = self._pending_queue[0]
try:
waiting_future = next(generator)
break
except StopIteration:
self._pending_queue.popleft()
continue
if waiting_future is None:
break
self._current_workers += 1
asyncio.ensure_future(waiting_future, loop=self._loop)
if self._idle():
async with self._waiter:
self._waiter.notify_all()
if exception is not None:
if callback_future is None:
raise exception
else:
callback_future.set_exception(exception)
return
if callback_future is None:
return ret
else:
callback_future.set_result(ret)
return
示例11: test_run_coroutine_job
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
async def test_run_coroutine_job(asyncio_scheduler, asyncio_executor, exception):
from asyncio import Future, sleep
future = Future()
job = asyncio_scheduler.add_job(waiter, 'interval', seconds=1, args=[sleep, exception])
asyncio_executor._run_job_success = lambda job_id, events: future.set_result(events)
asyncio_executor._run_job_error = lambda job_id, exc, tb: future.set_exception(exc)
asyncio_executor.submit_job(job, [datetime.now(utc)])
events = await future
assert len(events) == 1
if exception:
assert str(events[0].exception) == 'dummy error'
else:
assert events[0].retval is True
示例12: SubscriberThread
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
class SubscriberThread(threading.Thread):
def __init__(self, topic, id):
super().__init__(target=self)
self.subscriber = Subscriber(topic=topic, id=id)
self.future = Future()
def done(self):
return self.future.done()
def result(self):
return self.future.result()
def run(self):
try:
self.subscriber.open()
result = self.loop()
self.future.set_result(result)
except Exception as error:
self.future.set_exception(error)
finally:
self.subscriber.close()
def loop(self):
raise NotImplementedError()
示例13: func
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
def func():
future = Future()
future.set_exception(Exception(str(42)))
return future
示例14: _ChunkedResponse
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
class _ChunkedResponse(HTTPResponse):
def __init__(self, status, reason, msg, sock):
HTTPResponse.__init__(self, status, reason, msg)
self.sock = sock
self.chunk_read = None
sock.loop.create_task(self.Chunks())
self.size = None
async def read(self, amt):
if not self.size:
self.next_size = Future(loop=self.sock.loop)
if self.chunk_read:
self.chunk_read.set_result(None)
try:
[self.size, self.chunk_read] = await self.next_size
except EOFError:
return b""
data = await self.sock.recv(min(self.size, amt))
self.size -= len(data)
return data
async def Chunks(self):
"""
Generator that returns chunk length
"""
for _ in range(30000):
parser = Parser(self.sock)
await parser.next_char()
if parser.c == b"\r":
await parser.next_char()
if parser.c == b"\n":
await parser.next_char()
await parser.space()
size = 0
for _ in range(30):
try:
digit = int(parser.c, 16)
except ValueError:
break
size = size * 16 + digit
await parser.next_char()
else:
raise ExcessError("Chunk size of 30 or more digits")
i = 0
while parser.c not in b"\n": # Including EOF
i += 1
if i >= 3000:
raise ExcessError("Line of 3000 or more characters")
await parser.next_char()
if not size:
break
chunk_read = Future(loop=self.sock.loop)
self.next_size.set_result((size, chunk_read))
await chunk_read
else:
raise ExcessError("30000 or more chunks")
self.next_size.set_exception(EOFError())
await parser.headers()
示例15: _create_future_error
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import set_exception [as 别名]
def _create_future_error(error=None):
f = Future()
f.set_exception(error)
return f