Skip to content

Commit ecfad75

Browse files
authored
Merge pull request #5 from sorcio/trio-deprecation-warnings
update to Trio 0.9.0
2 parents 0ada777 + 900009b commit ecfad75

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
'pytest-trio >= 0.3',
1919
],
2020
install_requires=[
21-
'trio',
21+
'trio >= 0.9.0',
2222
],
2323
packages=[
2424
'trio_amqp',

trio_amqp/channel.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def __init__(self, channel, consumer_tag, **kwargs):
3232

3333
async def _data(self, channel, msg, env, prop):
3434
if msg is None:
35-
await self._q.put(None)
35+
await self._chan_send.send(None)
3636
else:
37-
await self._q.put((msg, env, prop))
37+
await self._chan_send.send((msg, env, prop))
3838

3939
if sys.version_info >= (3,5,3):
4040
def __aiter__(self):
@@ -44,14 +44,15 @@ async def __aiter__(self):
4444
return self
4545

4646
async def __anext__(self):
47-
res = await self._q.get()
47+
res = await self._chan_receive.receive()
4848
if res is None:
4949
raise StopAsyncIteration
5050
return res
5151

5252
async def __aenter__(self):
5353
await self.channel.basic_consume(self._data, consumer_tag=self.consumer_tag, **self.kwargs)
54-
self._q = trio.Queue(30) # TODO: 2 + possible prefetch
54+
# TODO: 2 + possible prefetch
55+
self._chan_send, self._chan_receive = trio.open_memory_channel(30)
5556
return self
5657

5758
async def __aexit__(self, *tb):
@@ -60,7 +61,8 @@ async def __aexit__(self, *tb):
6061
await self.channel.basic_cancel(self.consumer_tag)
6162
except AmqpClosedConnection:
6263
pass
63-
del self._q
64+
del self._chan_send
65+
del self._chan_receive
6466
# these messages are not acknowledged, thus deleting the queue will
6567
# not lose them
6668

@@ -75,7 +77,6 @@ def __iter__(self):
7577

7678

7779
class Channel:
78-
_q = None # for returned messages
7980

8081
def __init__(self, protocol, channel_id):
8182
self.protocol = protocol
@@ -97,9 +98,13 @@ def __init__(self, protocol, channel_id):
9798
self._futures = {}
9899
self._ctag_events = {}
99100

101+
self._chan_send = None
102+
self._chan_receive = None
103+
100104
def __aiter__(self):
101-
if self._q is None:
102-
self._q = trio.Queue(30) # TODO: 2 + possible prefetch
105+
if self._chan_send is None:
106+
# TODO: 2 + possible prefetch
107+
self._chan_send, self._chan_receive = trio.open_memory_channel(30)
103108
return self
104109

105110
if sys.version_info < (3,5,3):
@@ -108,7 +113,7 @@ async def __aiter__(self):
108113
return self._aiter()
109114

110115
async def __anext__(self):
111-
res = await self._q.get()
116+
res = await self._chan_receive.receive()
112117
if res is None:
113118
raise StopAsyncIteration
114119
return res
@@ -149,8 +154,8 @@ def connection_closed(self, server_code=None, server_reason=None, exception=None
149154

150155
self.protocol.release_channel_id(self.channel_id)
151156
self.close_event.set()
152-
if self._q is not None:
153-
self._q.put_nowait(None)
157+
if self._chan_send is not None:
158+
self._chan_send.send_nowait(None)
154159

155160
async def dispatch_frame(self, frame):
156161
methods = {
@@ -271,8 +276,8 @@ async def close(self, reply_code=0, reply_text="Normal Shutdown"):
271276
if not self.is_open:
272277
raise exceptions.ChannelClosed("channel already closed or closing")
273278
self.close_event.set()
274-
if self._q is not None:
275-
self._q.put_nowait(None)
279+
if self._chan_send is not None:
280+
self._chan_send.send_nowait(None)
276281
frame = amqp_frame.AmqpRequest(amqp_constants.TYPE_METHOD, self.channel_id)
277282
frame.declare_method(amqp_constants.CLASS_CHANNEL, amqp_constants.CHANNEL_CLOSE)
278283
request = amqp_frame.AmqpEncoder()
@@ -946,11 +951,11 @@ async def basic_return(self, frame):
946951
envelope = ReturnEnvelope(reply_code, reply_text,
947952
exchange_name, routing_key)
948953
properties = content_header_frame.properties
949-
if self._q is None:
954+
if self._chan_send is None:
950955
# they have set mandatory bit, but havent added a callback
951956
logger.warning("You don't iterate the channel for returned messages!")
952957
else:
953-
await self._q.put((body, envelope, properties))
958+
await self._chan_send.send((body, envelope, properties))
954959

955960
async def basic_get(self, queue_name='', no_ack=False):
956961
frame = amqp_frame.AmqpRequest(amqp_constants.TYPE_METHOD, self.channel_id)

trio_amqp/protocol.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ async def _drain(self):
202202
async def _write_frame(self, frame, encoder, drain=True):
203203
# Doesn't actually write frame, pushes it for _writer_loop task to
204204
# pick it up.
205-
await self._send_queue.put((frame, encoder))
205+
await self._send_send_channel.send((frame, encoder))
206206

207207
@trio.hazmat.enable_ki_protection
208208
async def _writer_loop(self, task_status=trio.TASK_STATUS_IGNORED):
@@ -216,15 +216,15 @@ async def _writer_loop(self, task_status=trio.TASK_STATUS_IGNORED):
216216
timeout = inf
217217

218218
with trio.move_on_after(timeout) as timeout_scope:
219-
frame, encoder = await self._send_queue.get()
219+
frame, encoder = await self._send_receive_channel.receive()
220220
if timeout_scope.cancelled_caught:
221221
await self.send_heartbeat()
222222
continue
223223

224224
f = frame.get_frame(encoder)
225225
try:
226226
await self._stream.send_all(f)
227-
except (trio.BrokenStreamError,trio.ClosedStreamError):
227+
except (trio.BrokenResourceError, trio.ClosedResourceError):
228228
# raise exceptions.AmqpClosedConnection(self) from None
229229
# the reader will raise the error also
230230
return
@@ -258,7 +258,7 @@ async def aclose(self, no_wait=False):
258258
encoder.write_short(0)
259259
try:
260260
await self._write_frame(frame, encoder)
261-
except trio.ClosedStreamError:
261+
except trio.BrokenResourceError:
262262
pass
263263
except Exception:
264264
logger.exception("Error while closing")
@@ -315,7 +315,7 @@ async def __aenter__(self):
315315
self.server_channel_max = None
316316
self.channels_ids_ceil = 0
317317
self.channels_ids_free = set()
318-
self._send_queue = trio.Queue(1)
318+
self._send_send_channel, self._send_receive_channel = trio.open_memory_channel(1)
319319

320320
if self._ssl:
321321
if self._ssl is True:
@@ -423,7 +423,7 @@ async def get_frame(self):
423423
frame = amqp_frame.AmqpResponse(self._stream)
424424
try:
425425
await frame.read_frame()
426-
except trio.BrokenStreamError:
426+
except trio.BrokenResourceError:
427427
raise exceptions.AmqpClosedConnection(self) from None
428428

429429
return frame
@@ -511,7 +511,7 @@ async def _reader_loop(self, task_status=trio.TASK_STATUS_IGNORED):
511511
with trio.fail_after(timeout):
512512
try:
513513
frame = await self.get_frame()
514-
except trio.ClosedStreamError:
514+
except (trio.BrokenResourceError, trio.ClosedResourceError):
515515
# the stream is now *really* closed …
516516
return
517517
try:

0 commit comments

Comments
 (0)