Skip to content

Closing consumer multiple times should not raise RunTimeError #678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2020

Conversation

mkmoisen
Copy link
Contributor

@mkmoisen mkmoisen commented Oct 1, 2019

The current behavior is to raise a RunTimeError if consumer.close is called more than once.

This proposed change makes close rerunnable by not raising RunTimeError if it is called more than once.

This will make it convenient for developers to safely call consumer.close in a finally or a context manager without having to trap this exception.

The use case is to make close rerunnable, for example if you wanted to make a context manager, or if you had a large function wrapped in a try/except/finally. Another programmer who calls your context manager can close inside if he wants; otherwise the context manager will close for him.

import contextlib
from confluent_kafka import Consumer

@contextlib.contextmanager
def kafka():
    consumer = Consumer({...})
    try:
        yield consumer
    finally:
        # currently, would need to wrap this in a try/except
        consumer.close()

# ...
# Some programmers may want to call close explicitly
with kafka() as consumer:
        # do something
        consumer.close()

# Other programmers would prefer the context manager to take care
with kafka() as consumer:
    # do something

I'm also under the impression that the majority of the other python libraries that close resources also make close rerunnable. I think it would be a good idea to have confluent_kafka follow this pattern.

from requests import Session
s = Session()
s.close()
s.close()  # does not raise an exception

import sqlite3
conn = sqlite3.connect(':memory:')
conn.close()
conn.close()  # does not raise an exception

@ghost
Copy link

ghost commented Oct 1, 2019

@confluentinc It looks like @mkmoisen just signed our Contributor License Agreement. 👍

Always at your service,

clabot

@rnpridgeon rnpridgeon merged commit e270507 into confluentinc:master May 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants