Skip to content

Commit

Permalink
Shutdown function for ensuring it's possible to unsubscribe before cl…
Browse files Browse the repository at this point in the history
…osing event loops (#186)
  • Loading branch information
lwis authored Oct 1, 2018
1 parent e4338c0 commit 1fce816
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions examples/example_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,7 @@ def observe_err_callback(err):
print("Try altering any light in the app, and watch the events!")
yield from asyncio.sleep(120)

yield from api.shutdown()


asyncio.get_event_loop().run_until_complete(run())
2 changes: 2 additions & 0 deletions examples/example_socket_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,7 @@ def observe_err_callback(err):
print("Waiting for observation to end (10 secs)")
yield from asyncio.sleep(10)

yield from api.shutdown()


asyncio.get_event_loop().run_until_complete(run())
18 changes: 11 additions & 7 deletions pytradfri/api/aiocoap_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Coap implementation using aiocoap."""
"""COAP implementation using aiocoap."""
import asyncio
import json
import logging
Expand All @@ -15,7 +15,8 @@


class PatchedDTLSSecurityStore:
"""Patched DTLS store in lieu of impl."""
"""Patched DTLS store in lieu of a credentials framework.
https://github.com/chrysn/aiocoap/issues/97"""

IDENTITY = None
KEY = None
Expand Down Expand Up @@ -72,8 +73,7 @@ def _get_protocol(self):

@asyncio.coroutine
def _reset_protocol(self, exc=None):
"""Reset the protocol if an error occurs.
This can be removed when chrysn/aiocoap#79 is closed."""
"""Reset the protocol if an error occurs."""
# Be responsible and clean up.
protocol = yield from self._get_protocol()
yield from protocol.shutdown()
Expand All @@ -83,6 +83,12 @@ def _reset_protocol(self, exc=None):
ob_error(exc)
self._observations_err_callbacks.clear()

@asyncio.coroutine
def shutdown(self, exc=None):
"""Shutdown the API events.
This should be called before closing the event loop."""
yield from self._reset_protocol(exc)

@asyncio.coroutine
def _get_response(self, msg):
"""Perform the request, get the response."""
Expand Down Expand Up @@ -184,9 +190,7 @@ def error_callback(ex):

@asyncio.coroutine
def generate_psk(self, security_key):
"""
Generate and set a psk from the security key.
"""
"""Generate and set a psk from the security key."""
if not self._psk:
PatchedDTLSSecurityStore.IDENTITY = 'Client_identity'.encode(
'utf-8')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()

VERSION = "5.6.0"
VERSION = "5.6.1"
DOWNLOAD_URL = \
'https://github.com/ggravlingen/pytradfri/archive/{}.zip'.format(VERSION)

Expand Down

0 comments on commit 1fce816

Please sign in to comment.