Skip to content

Commit

Permalink
Merge pull request #17 from ekerstens/context-manager-support
Browse files Browse the repository at this point in the history
Add context manager support
  • Loading branch information
ask authored Feb 25, 2020
2 parents 96ea83d + b84fe6e commit 10e4119
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions aiokafka/consumer/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@ def __del__(self, _warnings=warnings):
context['source_traceback'] = self._source_traceback
self._loop.call_exception_handler(context)

async def __aenter__(self):
await self.start()
return self

async def __aexit__(self, type, value, traceback):
await self.stop()

async def start(self):
""" Connect to Kafka cluster. This will:
Expand Down
7 changes: 7 additions & 0 deletions aiokafka/producer/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ def __del__(self, _warnings=warnings):
context['source_traceback'] = self._source_traceback
self._loop.call_exception_handler(context)

async def __aenter__(self):
await self.start()
return self

async def __aexit__(self, type, value, traceback):
await self.stop()

async def start(self):
"""Connect to Kafka cluster and check server version"""
log.debug("Starting the Kafka producer") # trace
Expand Down

0 comments on commit 10e4119

Please sign in to comment.