Skip to content

Commit 1f258f2

Browse files
Adds a loop property to SerialTransport.
Retrieve the event loop from the transport rather than relying on the vagaries of get_event_loop(). Tidies up the tests and example code to make it a bit clearer when the transport is set on the protocol and to avoid linting issues. Demonstrate how to retrieve the event loop from the transport in the documentation.
1 parent c7228d5 commit 1f258f2

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

documentation/shortintro.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
====================
2-
Short introduction
3-
====================
1+
==================
2+
Short introduction
3+
==================
44

55
Example::
66

@@ -18,7 +18,7 @@ Example::
1818

1919
def connection_lost(self, exc):
2020
print('port closed')
21-
asyncio.get_event_loop().stop()
21+
self.transport.loop.stop()
2222

2323
def pause_writing(self):
2424
print('pause writing')
@@ -33,3 +33,4 @@ Example::
3333
loop.run_until_complete(coro)
3434
loop.run_forever()
3535
loop.close()
36+

serial_asyncio/__init__.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,18 @@ def __init__(self, loop, protocol, serial_instance):
6767
loop.call_soon(protocol.connection_made, self)
6868
loop.call_soon(self._ensure_reader)
6969

70+
@property
71+
def loop(self):
72+
"""The asyncio event loop used by this SerialTransport."""
73+
return self._loop
74+
7075
@property
7176
def serial(self):
7277
"""The underlying Serial instance."""
7378
return self._serial
7479

7580
def __repr__(self):
76-
return '{self.__class__.__name__}({self._loop}, {self._protocol}, {self.serial})'.format(self=self)
81+
return '{self.__class__.__name__}({self.loop}, {self._protocol}, {self.serial})'.format(self=self)
7782

7883
def is_closing(self):
7984
"""Return True if the transport is closing or closed."""
@@ -446,27 +451,32 @@ def open_serial_connection(*,
446451
# test
447452
if __name__ == '__main__':
448453
class Output(asyncio.Protocol):
454+
455+
def __init__(self):
456+
super().__init__()
457+
self._transport = None
458+
449459
def connection_made(self, transport):
450-
self.transport = transport
451-
print('port opened', transport)
452-
transport.serial.rts = False
453-
transport.write(b'hello world\n')
460+
self._transport = transport
461+
print('port opened', self._transport)
462+
self._transport.serial.rts = False
463+
self._transport.write(b'Hello, World!\n')
454464

455465
def data_received(self, data):
456466
print('data received', repr(data))
457467
if b'\n' in data:
458-
self.transport.close()
468+
self._transport.close()
459469

460470
def connection_lost(self, exc):
461471
print('port closed')
462-
asyncio.get_event_loop().stop()
472+
self._transport.loop.stop()
463473

464474
def pause_writing(self):
465475
print('pause writing')
466-
print(self.transport.get_write_buffer_size())
476+
print(self._transport.get_write_buffer_size())
467477

468478
def resume_writing(self):
469-
print(self.transport.get_write_buffer_size())
479+
print(self._transport.get_write_buffer_size())
470480
print('resume writing')
471481

472482
loop = asyncio.get_event_loop()

test/test_asyncio.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,49 @@ def tearDown(self):
4040
self.loop.close()
4141

4242
def test_asyncio(self):
43-
TEXT = b'hello world\n'
43+
TEXT = b'Hello, World!\n'
4444
received = []
4545
actions = []
4646

4747
class Input(asyncio.Protocol):
48+
49+
def __init__(self):
50+
super().__init__()
51+
self._transport = None
52+
4853
def connection_made(self, transport):
49-
self.transport = transport
54+
self._transport = transport
5055

5156
def data_received(self, data):
52-
self.transport.write(data)
57+
self._transport.write(data)
5358

5459
class Output(asyncio.Protocol):
60+
61+
def __init__(self):
62+
super().__init__()
63+
self._transport = None
64+
5565
def connection_made(self, transport):
56-
self.transport = transport
66+
self._transport = transport
5767
actions.append('open')
5868
transport.write(TEXT)
5969

6070
def data_received(self, data):
6171
received.append(data)
6272
if b'\n' in data:
63-
self.transport.close()
73+
self._transport.close()
6474

6575
def connection_lost(self, exc):
6676
actions.append('close')
67-
asyncio.get_event_loop().stop()
77+
self._transport.loop.stop()
6878

6979
def pause_writing(self):
7080
actions.append('pause')
71-
print(self.transport.get_write_buffer_size())
81+
print(self._transport.get_write_buffer_size())
7282

7383
def resume_writing(self):
7484
actions.append('resume')
75-
print(self.transport.get_write_buffer_size())
85+
print(self._transport.get_write_buffer_size())
7686

7787
if PORT.startswith('socket://'):
7888
coro = self.loop.create_server(Input, HOST, _PORT)

0 commit comments

Comments
 (0)