本文整理汇总了Python中asyncio.Future.add_done_callback方法的典型用法代码示例。如果您正苦于以下问题:Python Future.add_done_callback方法的具体用法?Python Future.add_done_callback怎么用?Python Future.add_done_callback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asyncio.Future
的用法示例。
在下文中一共展示了Future.add_done_callback方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: start_tls
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import add_done_callback [as 别名]
def start_tls(self, server_side, ssl_options=None, server_hostname=None, connect_timeout=None):
if not self._transport or self._read_future:
raise ValueError("IOStream is not idle; cannot convert to SSL")
self._connect_ssl_future = connect_ssl_future = Future(loop=self._loop)
waiter = Future(loop=self._loop)
def on_connected(future):
if self._loop_connect_timeout:
self._loop_connect_timeout.cancel()
self._loop_connect_timeout = None
if connect_ssl_future._exception is not None:
self.on_closed(future.exception())
self._connect_ssl_future = None
else:
self._connect_ssl_future = None
connect_ssl_future.set_result(self)
waiter.add_done_callback(on_connected)
if connect_timeout:
def on_timeout():
self._loop_connect_timeout = None
if not waiter.done():
self.close((None, IOError("Connect timeout"), None))
self._loop_connect_timeout = self._loop.call_later(connect_timeout, on_timeout)
self._transport.pause_reading()
sock, self._transport._sock = self._transport._sock, None
self._transport = self._loop._make_ssl_transport(
sock, self, ssl_options, waiter,
server_side=False, server_hostname=server_hostname)
return connect_ssl_future
示例2: WebSocketAdapterProtocol
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import add_done_callback [as 别名]
class WebSocketAdapterProtocol(asyncio.Protocol):
"""
Adapter class for asyncio-based WebSocket client and server protocols.
"""
def connection_made(self, transport):
self.transport = transport
self.receive_queue = deque()
self._consume()
try:
peer = transport.get_extra_info('peername')
try:
# FIXME: tcp4 vs tcp6
self.peer = u"tcp:%s:%d" % (peer[0], peer[1])
except:
# e.g. Unix Domain sockets don't have host/port
self.peer = u"unix:{0}".format(peer)
except:
self.peer = u"?"
self._connectionMade()
def connection_lost(self, exc):
self._connectionLost(exc)
# according to asyncio docs, connection_lost(None) is called
# if something else called transport.close()
if exc is not None:
self.transport.close()
self.transport = None
def _consume(self):
self.waiter = Future(loop=self.factory.loop or txaio.config.loop)
def process(_):
while len(self.receive_queue):
data = self.receive_queue.popleft()
if self.transport:
self._dataReceived(data)
self._consume()
self.waiter.add_done_callback(process)
def data_received(self, data):
self.receive_queue.append(data)
if not self.waiter.done():
self.waiter.set_result(None)
# noinspection PyUnusedLocal
def _closeConnection(self, abort=False):
self.transport.close()
def _onOpen(self):
res = self.onOpen()
if yields(res):
ensure_future(res)
def _onMessageBegin(self, isBinary):
res = self.onMessageBegin(isBinary)
if yields(res):
ensure_future(res)
def _onMessageFrameBegin(self, length):
res = self.onMessageFrameBegin(length)
if yields(res):
ensure_future(res)
def _onMessageFrameData(self, payload):
res = self.onMessageFrameData(payload)
if yields(res):
ensure_future(res)
def _onMessageFrameEnd(self):
res = self.onMessageFrameEnd()
if yields(res):
ensure_future(res)
def _onMessageFrame(self, payload):
res = self.onMessageFrame(payload)
if yields(res):
ensure_future(res)
def _onMessageEnd(self):
res = self.onMessageEnd()
if yields(res):
ensure_future(res)
def _onMessage(self, payload, isBinary):
res = self.onMessage(payload, isBinary)
if yields(res):
ensure_future(res)
def _onPing(self, payload):
res = self.onPing(payload)
if yields(res):
ensure_future(res)
def _onPong(self, payload):
res = self.onPong(payload)
#.........这里部分代码省略.........
示例3: _Application
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import add_done_callback [as 别名]
#.........这里部分代码省略.........
# 3. Returns error messages. Here, actions sent to Circus come from
# direct interaction with the GUI, and errors messages are expected.
#
@coroutine
def _on_reply(self, action, name, on_reply_ok, on_reply_error):
def callback(future):
reply = future.result()
if query['id'] == reply['id'] and reply['status'] == 'ok':
on_reply_ok(reply)
elif query['id'] == reply['id']:
on_reply_error(reply['reason'].capitalize() + '.')
else:
raise Exception()
query = {'id': uuid4().hex, 'command': action}
query['properties'] = {'name': name}
if action == 'incr' or action == 'decr':
query['properties'].update({'waiting': False, 'nb': 1})
elif action == 'start' or action == 'stop':
query['properties'].update({'waiting': False, 'match': 'glob'})
# Block to prevent writing multiple requests before reading a reply.
if self._rep2:
yield from self._rep2
self._rep2 = Future()
self._rep2.add_done_callback(callback)
self._req2.write([dumps(query).encode()])
def _update_watcher_state_a(self, name): # See [LEAK].
lb1 = self._grid[name + '+l']
lb2 = self._grid[name + '+c1']
if lb1._w_singleton:
bt1 = self._grid[name + '+c2']
bt2 = self._grid[name + '+r']
if lb1._w_procs:
lb2.config(foreground='darkgreen')
bt1.config(text='Stop')
bt2.config(state='normal')
bt1.bind('<Button-1>', partial(self._stop_watcher, name))
bt2.bind('<Button-1>', partial(self._more_watcher, name))
else:
lb2.config(foreground='grey', text='–')
bt1.config(text='Start')
bt2.config(state='disabled')
bt1.bind('<Button-1>', partial(self._start_watcher, name))
bt2.unbind('<Button-1>')
else:
bt1 = self._grid[name + '+c2']
bt2 = self._grid[name + '+c3']
bt3 = self._grid[name + '+r']
示例4: WebSocketAdapterProtocol
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import add_done_callback [as 别名]
class WebSocketAdapterProtocol(asyncio.Protocol):
"""
Adapter class for asyncio-based WebSocket client and server protocols.
"""
def connection_made(self, transport):
self.transport = transport
self.receive_queue = deque()
self._consume()
try:
peer = transport.get_extra_info('peername')
try:
# FIXME: tcp4 vs tcp6
self.peer = "tcp:%s:%d" % (peer[0], peer[1])
except:
# e.g. Unix Domain sockets don't have host/port
self.peer = "unix:{0}".format(peer)
except:
self.peer = "?"
self._connectionMade()
def connection_lost(self, exc):
self._connectionLost(exc)
self.transport = None
def _consume(self):
self.waiter = Future()
def process(_):
while len(self.receive_queue):
data = self.receive_queue.popleft()
if self.transport:
self._dataReceived(data)
else:
print("WebSocketAdapterProtocol._consume: no transport")
self._consume()
self.waiter.add_done_callback(process)
def data_received(self, data):
self.receive_queue.append(data)
if not self.waiter.done():
self.waiter.set_result(None)
# noinspection PyUnusedLocal
def _closeConnection(self, abort=False):
self.transport.close()
def _onOpen(self):
res = self.onOpen()
if yields(res):
asyncio.async(res)
def _onMessageBegin(self, isBinary):
res = self.onMessageBegin(isBinary)
if yields(res):
asyncio.async(res)
def _onMessageFrameBegin(self, length):
res = self.onMessageFrameBegin(length)
if yields(res):
asyncio.async(res)
def _onMessageFrameData(self, payload):
res = self.onMessageFrameData(payload)
if yields(res):
asyncio.async(res)
def _onMessageFrameEnd(self):
res = self.onMessageFrameEnd()
if yields(res):
asyncio.async(res)
def _onMessageFrame(self, payload):
res = self.onMessageFrame(payload)
if yields(res):
asyncio.async(res)
def _onMessageEnd(self):
res = self.onMessageEnd()
if yields(res):
asyncio.async(res)
def _onMessage(self, payload, isBinary):
res = self.onMessage(payload, isBinary)
if yields(res):
asyncio.async(res)
def _onPing(self, payload):
res = self.onPing(payload)
if yields(res):
asyncio.async(res)
def _onPong(self, payload):
res = self.onPong(payload)
if yields(res):
asyncio.async(res)
#.........这里部分代码省略.........
示例5: WebSocketAdapterProtocol
# 需要导入模块: from asyncio import Future [as 别名]
# 或者: from asyncio.Future import add_done_callback [as 别名]
class WebSocketAdapterProtocol(asyncio.Protocol):
"""
Adapter class for asyncio-based WebSocket client and server protocols.
"""
def connection_made(self, transport):
self.transport = transport
self.receive_queue = deque()
self._consume()
try:
self.peer = peer2str(transport.get_extra_info('peername'))
except:
self.peer = u"?"
self._connectionMade()
def connection_lost(self, exc):
self._connectionLost(exc)
# according to asyncio docs, connection_lost(None) is called
# if something else called transport.close()
if exc is not None:
self.transport.close()
self.transport = None
def _consume(self):
self.waiter = Future(loop=self.factory.loop or txaio.config.loop)
def process(_):
while len(self.receive_queue):
data = self.receive_queue.popleft()
if self.transport:
self._dataReceived(data)
self._consume()
self.waiter.add_done_callback(process)
def data_received(self, data):
self.receive_queue.append(data)
if not self.waiter.done():
self.waiter.set_result(None)
def _closeConnection(self, abort=False):
if abort and hasattr(self.transport, 'abort'):
self.transport.abort()
else:
self.transport.close()
def _onOpen(self):
res = self.onOpen()
if yields(res):
ensure_future(res)
def _onMessageBegin(self, isBinary):
res = self.onMessageBegin(isBinary)
if yields(res):
ensure_future(res)
def _onMessageFrameBegin(self, length):
res = self.onMessageFrameBegin(length)
if yields(res):
ensure_future(res)
def _onMessageFrameData(self, payload):
res = self.onMessageFrameData(payload)
if yields(res):
ensure_future(res)
def _onMessageFrameEnd(self):
res = self.onMessageFrameEnd()
if yields(res):
ensure_future(res)
def _onMessageFrame(self, payload):
res = self.onMessageFrame(payload)
if yields(res):
ensure_future(res)
def _onMessageEnd(self):
res = self.onMessageEnd()
if yields(res):
ensure_future(res)
def _onMessage(self, payload, isBinary):
res = self.onMessage(payload, isBinary)
if yields(res):
ensure_future(res)
def _onPing(self, payload):
res = self.onPing(payload)
if yields(res):
ensure_future(res)
def _onPong(self, payload):
res = self.onPong(payload)
if yields(res):
ensure_future(res)
def _onClose(self, wasClean, code, reason):
#.........这里部分代码省略.........