本文整理汇总了Python中asyncio.queues.Queue类的典型用法代码示例。如果您正苦于以下问题:Python Queue类的具体用法?Python Queue怎么用?Python Queue使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Queue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: IRCClientProtocol
class IRCClientProtocol(asyncio.Protocol):
"""Low-level protocol that speaks the client end of IRC.
This isn't responsible for very much besides the barest minimum definition
of an IRC client: connecting and responding to PING.
You probably want `read_message`, or the higher-level client class.
"""
def __init__(self, loop, nick, password, charset="utf8"):
self.nick = nick
self.password = password
self.charset = charset
self.buf = b""
self.message_queue = Queue(loop=loop)
self.registered = False
def connection_made(self, transport):
self.transport = transport
if self.password:
self.send_message("PASS", self.password)
self.send_message("NICK", self.nick)
self.send_message("USER", "dywypi", "-", "-", "dywypi Python IRC bot")
def data_received(self, data):
data = self.buf + data
while True:
raw_message, delim, data = data.partition(b"\r\n")
if not delim:
# Incomplete message; stop here and wait for more
self.buf = raw_message
return
# TODO valerr
message = IRCMessage.parse(raw_message.decode(self.charset))
logger.debug("recv: %r", message)
self.handle_message(message)
def handle_message(self, message):
if message.command == "PING":
self.send_message("PONG", message.args[-1])
elif message.command == "RPL_WELCOME":
# 001, first thing sent after registration
if not self.registered:
self.registered = True
self.message_queue.put_nowait(message)
def send_message(self, command, *args):
message = IRCMessage(command, *args)
logger.debug("sent: %r", message)
self.transport.write(message.render().encode(self.charset) + b"\r\n")
@asyncio.coroutine
def read_message(self):
return (yield from self.message_queue.get())
示例2: _serve
async def _serve(self, websocket, path):
import websockets
connection_id = await websocket.recv()
with self.queue_lock:
#if connection_id in self._preclosed_connections:
# self._preclosed_connections[connection_id].set()
# return
pmqueue = self._pending_message_queues.get(connection_id, None)
myqueue = Queue()
if pmqueue is not None:
events = []
while 1:
try:
e = pmqueue.get_nowait()
except QueueEmpty:
break
events.append(e)
if len(events) > self.CACHE_EVENTS_FIRST + self.CACHE_EVENTS_LAST:
events = events[:self.CACHE_EVENTS_FIRST] + \
events[-self.CACHE_EVENTS_LAST:]
for enr, e in enumerate(events):
swallow = False
if e.get("type", None) == "var":
varname = e.get("var", None)
if varname is not None:
for e2 in events[enr+1:]:
if e2.get("type", None) != "var":
continue
if e2.get("var", None) != varname:
continue
swallow = True
break
if swallow:
continue
myqueue.put_nowait(e)
pmqueue.put_nowait(e) #put the events back
if connection_id not in self._message_queue_lists:
self._message_queue_lists[connection_id] = []
self._message_queue_lists[connection_id].append(myqueue)
while True:
#print("WAIT")
try:
message = await myqueue.get()
except Exception:
break
message = json.dumps(message)
#print("SEND?", message)
if message is None: #terminating message
break
try:
#print("SEND", message)
await websocket.send(message)
except websockets.exceptions.ConnectionClosed:
break
with self.queue_lock:
self._message_queue_lists[connection_id].remove(myqueue)
示例3: Core
class Core(object):
def __init__(self, bot):
self.bot = bot
self.timeout = int(self.bot.config.get('timeout'))
self.ping_queue = Queue(loop=bot.loop)
def connection_made(self):
self.bot.loop.call_later(self.timeout, self.check_ping)
self.ping_queue.put_nowait(self.bot.loop.time())
def check_ping(self): # pragma: no cover
# check if we received a ping
# reconnect if queue is empty
self.bot.log.debug(
'Ping queue size: {}'.format(self.ping_queue.qsize()))
if self.ping_queue.empty():
self.bot.loop.call_soon(self.bot.protocol.transport.close)
else:
self.bot.loop.call_later(self.timeout, self.check_ping)
while not self.ping_queue.empty():
self.ping_queue.get_nowait()
@event(rfc.PING)
def pong(self, data):
"""PING reply"""
self.ping_queue.put_nowait(self.bot.loop.time())
self.bot.send('PONG ' + data)
@event(rfc.NEW_NICK)
def recompile(self, nick=None, new_nick=None, **kw):
"""recompile regexp on new nick"""
if self.bot.nick == nick.nick:
self.bot.config['nick'] = new_nick
self.bot.recompile()
@event(rfc.ERR_NICK)
def badnick(self, me=None, nick=None, **kw):
"""Use alt nick on nick error"""
if me == '*':
self.bot.set_nick(self.bot.nick + '_')
self.bot.log.debug('Trying to regain nickname in 30s...')
self.bot.loop.call_later(30, self.bot.set_nick, self.bot.original_nick)
@event(rfc.RPL_ENDOFMOTD)
def autojoin(self, **kw):
"""autojoin at the end of MOTD"""
self.bot.config['nick'] = kw['me']
self.bot.recompile()
channels = utils.as_list(self.bot.config.get('autojoins', []))
for channel in channels:
channel = utils.as_channel(channel)
self.bot.log.info('Trying to join %s', channel)
self.bot.join(channel)
示例4: __init__
def __init__(self, *,
host=None, port=None, secure=None, timeout=10, max_size=2 ** 20, loop=None):
self.host = host
self.port = port
self.secure = secure
self.timeout = timeout
self.max_size = max_size
super().__init__(asyncio.StreamReader(), self.client_connected, loop)
self.close_code = None
self.close_reason = ''
# Futures tracking steps in the connection's lifecycle.
self.opening_handshake = asyncio.Future()
self.closing_handshake = asyncio.Future()
self.connection_failed = asyncio.Future()
self.connection_closed = asyncio.Future()
# Queue of received messages.
self.messages = Queue()
# Mapping of ping IDs to waiters, in chronological order.
self.pings = collections.OrderedDict()
# Task managing the connection.
self.worker = asyncio.async(self.run())
# In a subclass implementing the opening handshake, the state will be
# CONNECTING at this point.
if self.state == 'OPEN':
self.opening_handshake.set_result(True)
示例5: __init__
def __init__(self, loop, network):
self.loop = loop
self.network = network
self.joined_channels = {} # name => Channel
# IRC server features, as reported by ISUPPORT, with defaults taken
# from the RFC.
self.len_nick = 9
self.len_channel = 200
self.len_message = 510
# These lengths don't have limits mentioned in the RFC, so going with
# the smallest known values in the wild
self.len_kick = 80
self.len_topic = 80
self.len_away = 160
self.max_watches = 0
self.max_targets = 1
self.channel_types = set('#&')
self.channel_modes = {} # TODO, haha.
self.channel_prefixes = {} # TODO here too. IRCMode is awkward.
self.network_title = self.network.name
self.features = {}
# Various intermediate state used for waiting for replies and
# aggregating multi-part replies
# TODO hmmm so what happens if state just gets left here forever? do
# we care?
self._pending_names = {}
self._names_futures = {}
self._pending_topics = {}
self._join_futures = {}
self.event_queue = Queue(loop=loop)
示例6: __init__
def __init__(self, sr, sw, *, loop=None):
if not loop:
loop = asyncio.get_event_loop()
self._loop = loop
self._sr = sr
self._sw = sw
self._msgs = Queue(loop=loop)
self._worker = loop.create_task(self._run())
示例7: __init__
def __init__(self, loop, nick, password, charset="utf8"):
self.nick = nick
self.password = password
self.charset = charset
self.buf = b""
self.message_queue = Queue(loop=loop)
self.registered = False
示例8: IrcConnection
class IrcConnection(asyncio.Protocol):
"""asyncio protocol to handle an irc connection"""
def connection_made(self, transport):
self.transport = transport
self.closed = False
self.queue = Queue()
def data_received(self, data):
encoding = getattr(self, 'encoding', 'ascii')
data = data.decode(encoding, 'ignore')
if not self.queue.empty():
data = self.queue.get_nowait() + data
lines = data.split('\r\n')
self.queue.put_nowait(lines.pop(-1))
for line in lines:
self.factory.dispatch(line)
def write(self, data):
if data is not None:
if isinstance(data, text_type):
data = data.encode(self.encoding)
if not data.endswith(b'\r\n'):
data = data + b'\r\n'
self.transport.write(data)
def connection_lost(self, exc): # pragma: no cover
self.factory.log.critical('connection lost (%s): %r',
id(self.transport),
exc)
self.factory.notify('connection_lost')
if not self.closed:
self.close()
# wait a few before reconnect
self.factory.loop.call_later(
2, self.factory.create_connection, self.__class__)
def close(self): # pragma: no cover
if not self.closed:
self.factory.log.critical('closing old transport (%r)',
id(self.transport))
try:
self.transport.close()
finally:
self.closed = True
示例9: __init__
def __init__(self, loop, network, *args, **kwargs):
super().__init__(loop, **kwargs)
self.event_queue = Queue(loop=self.loop)
self.network = network
self.me = Peer('dywypi', 'dywypi', 'localhost')
self.you = Peer('user', 'user', 'localhost')
示例10: __init__
def __init__(self, loop, network):
self.loop = loop
# TODO it would be nice to parametrize these (or even accept arbitrary
# transports), but the event loop doesn't support async reading from
# ttys for some reason...
self.stdin = sys.stdin
self.stdout = sys.stdout
self.event_queue = Queue(loop=loop)
示例11: sending_loop
def sending_loop(websocket):
# create sending-queue
loop = asyncio.get_event_loop()
sending_queue = Queue()
logger.info("websockets .... Queue startet")
def changed(tmp):
loop.call_soon_threadsafe(sending_queue.put_nowait, tmp)
try:
consumers.append(changed)
logger.info("websockets .... consumers.append")
while True:
tmp_data = yield from sending_queue.get()
yield from websocket.send(tmp_data)
logger.debug("websockets .... yield from websocket.send : %s" % tmp_data)
finally:
consumers.remove(changed)
logger.info("websockets .... consumers.remove")
示例12: sending_loop_gui
def sending_loop_gui(websocket):
# create sending-queue
loop = asyncio.get_event_loop()
sending_queue_gui = Queue()
logger.info('websockets .... GUI Queue startet')
def changed(tmp):
loop.call_soon_threadsafe(sending_queue_gui.put_nowait, tmp)
try:
consumers_gui.append(changed)
logger.info('websockets .... ein GUI-Client wurde in die Queue aufgenommen')
while True:
tmp_data = yield from sending_queue_gui.get()
yield from websocket.send(tmp_data)
logger.debug('websockets .... Sende json Daten -> GUI : %s' % tmp_data)
finally:
consumers_gui.remove(changed)
logger.info('websockets .... ein GUI-Client wurde aus der Queue entfernt')
示例13: _handler
async def _handler(self, websocket, path):
print('[WebSocketThread] Incoming connection')
queue = Queue()
async def send_message_async(message):
await queue.put(message)
def send_message(message):
asyncio.run_coroutine_threadsafe(send_message_async(message), self._loop)
def close():
send_message(None)
on_open, on_message, on_close = self._accept_connection(send_message, close)
on_open()
listener_task = asyncio.ensure_future(websocket.recv())
producer_task = asyncio.ensure_future(queue.get())
try:
while True:
done, pending = await asyncio.wait(
[listener_task, producer_task],
return_when=asyncio.FIRST_COMPLETED)
if listener_task in done:
message = listener_task.result()
on_message(message)
listener_task = asyncio.ensure_future(websocket.recv())
if producer_task in done:
message = producer_task.result()
if message is None:
break
producer_task = asyncio.ensure_future(queue.get())
await websocket.send(message)
finally:
listener_task.cancel()
producer_task.cancel()
on_close()
print('[WebSocketThread] Connection closed')
示例14: register
def register(self, channel):
"""
Handler usage:
>>> channel = yield from dispatcher.register('foo')
>>> yield from channel.get()
>>> channel.close()
"""
q = Queue()
self.queues[channel].append(q)
yield from self.subscription.subscribe([channel])
def free(*a, **k):
"""
Stop serving client
"""
queues = self.queues[channel]
queues.remove(q)
if not queues:
yield from self.subscription.unsubscribe([channel])
del self.queues[channel]
logger.info('%s chanel was released', channel)
q.close = free
return q
示例15: __init__
def __init__(self, timeout=10):
self.timeout = timeout
self.close_code = None
self.close_reason = ''
# Futures tracking steps in the connection's lifecycle.
self.opening_handshake = asyncio.Future()
self.closing_handshake = asyncio.Future()
self.connection_closed = asyncio.Future()
# Queue of received messages.
self.messages = Queue()
# Mapping of ping IDs to waiters, in chronological order.
self.pings = collections.OrderedDict()
# Task managing the connection.
self.worker = asyncio.async(self.run())
# In a subclass implementing the opening handshake, the state will be
# CONNECTING at this point.
if self.state == 'OPEN':
self.opening_handshake.set_result(True)