本文整理汇总了Python中asyncio.queues.Queue.get方法的典型用法代码示例。如果您正苦于以下问题:Python Queue.get方法的具体用法?Python Queue.get怎么用?Python Queue.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asyncio.queues.Queue
的用法示例。
在下文中一共展示了Queue.get方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: IRCClientProtocol
# 需要导入模块: from asyncio.queues import Queue [as 别名]
# 或者: from asyncio.queues.Queue import get [as 别名]
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: _handler
# 需要导入模块: from asyncio.queues import Queue [as 别名]
# 或者: from asyncio.queues.Queue import get [as 别名]
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')
示例3: sending_loop_gui
# 需要导入模块: from asyncio.queues import Queue [as 别名]
# 或者: from asyncio.queues.Queue import get [as 别名]
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')
示例4: sending_loop
# 需要导入模块: from asyncio.queues import Queue [as 别名]
# 或者: from asyncio.queues.Queue import get [as 别名]
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")
示例5: __init__
# 需要导入模块: from asyncio.queues import Queue [as 别名]
# 或者: from asyncio.queues.Queue import get [as 别名]
class ShellClient:
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)
@asyncio.coroutine
def connect(self):
self.protocol = UrwidTerminalProtocol(DywypiShell, self.loop)
self.transport = TrivialFileTransport(self.loop, self.stdin, self.stdout, self.protocol)
@asyncio.coroutine
def disconnect(self):
self.protocol.bridge.stop()
@asyncio.coroutine
def read_event(self):
# For now, this will never ever do anything.
# TODO this sure looks a lot like IRCClient
return (yield from self.event_queue.get())
def format_transition(self, current_style, new_style):
if new_style == Style.default():
# Just use the reset sequence
return "\x1b[0m"
ret = ""
if new_style.fg != current_style.fg:
ret += FOREGROUND_CODES[new_style.fg]
if new_style.bold != current_style.bold:
ret += BOLD_CODES[new_style.bold]
return ret
示例6: events
# 需要导入模块: from asyncio.queues import Queue [as 别名]
# 或者: from asyncio.queues.Queue import get [as 别名]
class IRCClient:
"""Higher-level IRC client. Takes care of most of the hard parts of IRC:
incoming server messages are bundled into more intelligible events (see
``dywypi.event``), and commands that expect replies are implemented as
coroutines.
"""
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)
def get_channel(self, channel_name):
"""Returns a `Channel` object containing everything the client
definitively knows about the given channel.
Note that if you, say, ask for the topic of a channel you aren't in and
then immediately call `get_channel`, the returned object won't have its
topic populated. State is only tracked persistently for channels the
bot is in; otherwise there's no way to know whether or not it's stale.
"""
if channel_name in self.joined_channels:
return self.joined_channels[channel_name]
else:
return IRCChannel(self, channel_name)
@asyncio.coroutine
def connect(self):
"""Coroutine for connecting to a single server.
Note that this will nonblock until the client is "registered", defined
as the first PING/PONG exchange.
"""
# TODO this is a poor excuse for round-robin :)
server = self.current_server = self.network.servers[0]
# TODO i'm pretty sure the server tells us what our nick is, and we
# should believe that instead
self.nick = self.network.preferred_nick
# TODO: handle disconnection, somehow. probably affects a lot of
# things.
# TODO kind of wish this weren't here, since the creation of the
# connection isn't inherently part of a client. really it should be on
# the... network, perhaps? and there's no reason i shouldn't be able
# to "connect" to a unix socket or pipe or anywhere else that has data.
_, self.proto = yield from self.loop.create_connection(
lambda: IRCClientProtocol(
self.loop, self.network.preferred_nick, password=server.password),
server.host, server.port, ssl=server.tls)
while True:
yield from self._read_message()
# TODO this is dumb garbage; more likely this client itself should
# just wait for 001/RPL_WELCOME.
if self.proto.registered:
break
# Start the event loop as soon as we've synched, or we can't respond to
# anything
asyncio.async(self._advance(), loop=self.loop)
# Initial joins
yield from asyncio.gather(*[
self.join(channel_name)
for channel_name in self.network.autojoins
], loop=self.loop)
@asyncio.coroutine
def disconnect(self):
#.........这里部分代码省略.........
示例7: __init__
# 需要导入模块: from asyncio.queues import Queue [as 别名]
# 或者: from asyncio.queues.Queue import get [as 别名]
class DCCClient:
def __init__(self, loop, network, send=False):
self.loop = loop
self.network = network
self.read_queue = Queue(loop=loop)
self.send = send #ugh what if i want to RECEIVE though.
#not sure what the use case would be but...?
@asyncio.coroutine
def connect(self, port=None):
if not self.send:
server = self.current_server = self.network.servers[0]
self._reader, self._writer = yield from server.connect(self.loop)
self._read_loop_task = asyncio.Task(self._start_read_loop())
asyncio.async(self._read_loop_task, loop=self.loop)
else:
self._waiting = asyncio.Lock()
yield from self._waiting.acquire()
if port:
self.network = yield from asyncio.start_server(self._handle_client,
host=socket.gethostbyname(socket.gethostname()), port=port, loop=self.loop)
else:
logger.error("No port provided for send")
@asyncio.coroutine
def _handle_client(self, client_reader, client_writer):
self._reader = client_reader
self._writer = client_writer
self._waiting.release()
self._read_loop_task = asyncio.Task(self._start_read_loop())
asyncio.async(self._read_loop_task, loop=self.loop)
@asyncio.coroutine
def disconnect(self):
yield from self._writer.drain()
self._writer.write_eof()
self._read_loop_task.cancel()
yield from self._read_loop_task
while not self._reader.at_eof():
yield from self._reader.readline()
if self.send:
self.network.close()
@asyncio.coroutine
def _start_read_loop(self):
if not self.send: #acks don't really do anything so don't listen for them
while not self._reader.at_eof():
try:
yield from self._read_message()
except CancelledError:
return
except Exception:
logger.exception("Smothering exception in DCC read loop")
@asyncio.coroutine
def _read_message(self):
line = yield from self._reader.readline()
m = re.match(b'(.*)(\r|\n|\r\n)$', line)
assert m
line = m.group(1)
message = DCCMessage.parse(line)
logger.debug("recv: %r", message)
event = DirectMessage(self, message)
self.read_queue.put_nowait((message, event))
@asyncio.coroutine
def read_event(self):
message, event = yield from self.read_queue.get()
return event
@asyncio.coroutine
def say(self, message, target=None, no_respond=None):
self.send_message(message)
@asyncio.coroutine
def send_message(self, message):
message = DCCMessage(message)
logger.debug("sent: %r", message)
self._writer.write(message.render().encode('utf8') + b'\r\n')
@asyncio.coroutine
def transfer(self, path):
yield from self._waiting.acquire()
f = open(str(path), 'rb')
block = b'\x01'
while block != b'':
block = f.read(1024)
self._writer.write(block)
f.close()
self._waiting.release()
return True
示例8: __init__
# 需要导入模块: from asyncio.queues import Queue [as 别名]
# 或者: from asyncio.queues.Queue import get [as 别名]
class StreamConnection:
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())
@asyncio.coroutine
def _run(self):
while self.alive():
try:
data = yield from self._sr.readline()
if data and len(data):
self._msgs.put_nowait(self._convert(data))
except asyncio.CancelledError:
logger.debug("readline from stream reader was cancelled.")
except ConnectionError:
logger.debug("connection error")
break
logger.debug("connection closed")
def _convert(self, data):
return data.strip()
@asyncio.coroutine
def recv(self):
try:
return self._msgs.get_nowait()
except QueueEmpty:
pass
# Wait for a message until the connection is closed
next_message = self._loop.create_task(self._msgs.get())
done, pending = yield from asyncio.wait(
[next_message, self._worker],
loop=self._loop, return_when=asyncio.FIRST_COMPLETED)
if next_message in done:
return next_message.result()
else:
next_message.cancel()
def send(self, data):
if not self.alive():
raise ConnectionError("connection was closed.")
try:
data = data + b'\n'
self._sw.write(data)
except OSError:
raise ConnectionError("can't send data.")
except Exception:
logger.debug("Q___Q")
def alive(self):
return not self._sr.at_eof()
@asyncio.coroutine
def drain():
yield from self._sw.drain()
@asyncio.coroutine
def close(self):
if self.alive():
try:
yield from self._sw.drain()
self._sw.write_eof()
except ConnectionError:
pass
else:
self._sr.feed_eof()
self._sw.close()
self._worker.cancel()
示例9: WebSocketCommonProtocol
# 需要导入模块: from asyncio.queues import Queue [as 别名]
# 或者: from asyncio.queues.Queue import get [as 别名]
#.........这里部分代码省略.........
The `code` must be an :class:`int` and the `reason` a :class:`str`.
"""
if self.state == 'OPEN':
# 7.1.2. Start the WebSocket Closing Handshake
self.close_code, self.close_reason = code, reason
yield from self.write_frame(OP_CLOSE, serialize_close(code, reason))
# 7.1.3. The WebSocket Closing Handshake is Started
self.state = 'CLOSING'
# If the connection doesn't terminate within the timeout, break out of
# the worker loop.
try:
yield from asyncio.wait_for(self.worker, timeout=self.timeout)
except asyncio.TimeoutError:
self.worker.cancel()
# The worker should terminate quickly once it has been cancelled.
yield from self.worker
@asyncio.coroutine
def recv(self):
"""
This coroutine receives the next message.
It returns a :class:`str` for a text frame and :class:`bytes` for a
binary frame.
When the end of the message stream is reached, or when a protocol
error occurs, :meth:`recv` returns ``None``, indicating that the
connection is closed.
"""
# Return any available message
try:
return self.messages.get_nowait()
except QueueEmpty:
pass
# Wait for a message until the connection is closed
next_message = asyncio.async(self.messages.get())
done, pending = yield from asyncio.wait(
[next_message, self.worker],
return_when=asyncio.FIRST_COMPLETED)
if next_message in done:
return next_message.result()
else:
next_message.cancel()
@asyncio.coroutine
def send(self, data):
"""
This coroutine sends a message.
It sends a :class:`str` as a text frame and :class:`bytes` as a binary
frame.
It raises a :exc:`TypeError` for other inputs and
:exc:`InvalidState` once the connection is closed.
"""
if isinstance(data, str):
opcode = 1
data = data.encode('utf-8')
elif isinstance(data, bytes):
opcode = 2
else:
raise TypeError("data must be bytes or str")
yield from self.write_frame(opcode, data)
示例10: events
# 需要导入模块: from asyncio.queues import Queue [as 别名]
# 或者: from asyncio.queues.Queue import get [as 别名]
class IRCClient:
"""Higher-level IRC client. Takes care of most of the hard parts of IRC:
incoming server messages are bundled into more intelligible events (see
``dywypi.event``), and commands that expect replies are implemented as
coroutines.
"""
def __init__(self, loop, network):
self.loop = loop
self.network = network
# TODO should this be a param? a property of the network? or, more
# likely, channel-specific and decoded separately and...
self.charset = "utf8"
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._message_waiters = deque()
self.read_queue = Queue(loop=loop)
def get_channel(self, channel_name):
"""Returns a `Channel` object containing everything the client
definitively knows about the given channel.
Note that if you, say, ask for the topic of a channel you aren't in and
then immediately call `get_channel`, the returned object won't have its
topic populated. State is only tracked persistently for channels the
bot is in; otherwise there's no way to know whether or not it's stale.
"""
if channel_name in self.joined_channels:
return self.joined_channels[channel_name]
else:
return IRCChannel(self, channel_name)
@asyncio.coroutine
def connect(self):
"""Coroutine for connecting to a single server.
Note that this will nonblock until the client is "registered", defined
as the first PING/PONG exchange.
"""
# TODO this is a poor excuse for round-robin :)
server = self.current_server = self.network.servers[0]
# TODO i'm pretty sure the server tells us what our nick is, and we
# should believe that instead
self.nick = self.network.preferred_nick
# TODO: handle disconnection, somehow. probably affects a lot of
# things.
self._reader, self._writer = yield from server.connect(self.loop)
if server.password:
self.send_message("PASS", server.password)
self.send_message("NICK", self.nick)
self.send_message("USER", "dywypi", "-", "-", "dywypi Python IRC bot")
# Start the reader loop, or we can't respond to anything
self._read_loop_task = asyncio.Task(self._start_read_loop())
asyncio.async(self._read_loop_task, loop=self.loop)
@asyncio.coroutine
def disconnect(self):
# Quit
self.send_message("QUIT", "Seeya!")
# Flush the write buffer
yield from self._writer.drain()
self._writer.close()
# Stop reading events
self._read_loop_task.cancel()
# This looks a little funny since this task is already running, but we
#.........这里部分代码省略.........
示例11: events
# 需要导入模块: from asyncio.queues import Queue [as 别名]
# 或者: from asyncio.queues.Queue import get [as 别名]
class IRCClient:
"""Higher-level IRC client. Takes care of most of the hard parts of IRC:
incoming server messages are bundled into more intelligible events (see
``dywypi.event``), and commands that expect replies are implemented as
coroutines.
"""
def __init__(self, loop, network):
self.loop = loop
self.network = network
# TODO should this be a param? a property of the network? or, more
# likely, channel-specific and decoded separately and...
self.charset = 'utf8'
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._message_waiters = OrderedDict()
self.read_queue = Queue(loop=loop)
def get_channel(self, channel_name):
"""Returns a `Channel` object containing everything the client
definitively knows about the given channel.
Note that if you, say, ask for the topic of a channel you aren't in and
then immediately call `get_channel`, the returned object won't have its
topic populated. State is only tracked persistently for channels the
bot is in; otherwise there's no way to know whether or not it's stale.
"""
if channel_name in self.joined_channels:
return self.joined_channels[channel_name]
else:
return IRCChannel(self, channel_name)
@asyncio.coroutine
def connect(self):
"""Coroutine for connecting to a single server.
Note that this will nonblock until the client is "registered", defined
as the first PING/PONG exchange.
"""
# TODO this is a poor excuse for round-robin :)
server = self.current_server = self.network.servers[0]
# TODO i'm pretty sure the server tells us what our nick is, and we
# should believe that instead
self.nick = self.network.preferred_nick
# TODO: handle disconnection, somehow. probably affects a lot of
# things.
self._reader, self._writer = yield from server.connect(self.loop)
log.debug('connected!')
if server.password:
self.send_message('PASS', server.password)
self.send_message('NICK', self.nick)
self.send_message('USER', 'dywypi', '-', '-', 'dywypi Python IRC bot')
# Start the reader loop, or we can't respond to anything
self._read_loop_task = asyncio.Task(self._start_read_loop())
asyncio.async(self._read_loop_task, loop=self.loop)
@asyncio.coroutine
def disconnect(self):
# Quit
self.send_message('QUIT', 'Seeya!')
# Flush the write buffer
yield from self._writer.drain()
self._writer.close()
# Stop reading events
self._read_loop_task.cancel()
#.........这里部分代码省略.........