Skip to content
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

feat: add option for persiting updated locals between connections #111

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pbabics
Copy link

@pbabics pbabics commented Oct 24, 2023

Hello,

I've added option for sharing locals between different connections allowing for persisting some of the state.

Example scenario

<connect>
profile = cProfile.Profile()
profile.enable()
<disconnect>

after some time

<connection>
profile.disable()
profile.print_stats()

@vxgmichel
Copy link
Owner

vxgmichel commented Oct 24, 2023

Hi @pbabics and thanks for your contribution!

Funnily enough I explicitly removed this behavior in PR #70, because I thought that getting a clean state for each connection was the best way to avoid breaking the console by mistake (like in issue #69). But as you pointed out, this might also be the desired behavior in some cases.

The problem with the shared dict approach you proposed is that starting a new console breaks the previous one since print and other locals will be overwritten. Anything printed in the context of the first console will effectively appear in the second one.

This can probably be fixed by using contextvars in those print, help and ainput methods so they use the streams in the current context instead of using attributes in self.

I'm also thinking about the API.. The boolean shared_locals switch approach you used seems fine, but I wonder if we can do better. Maybe there's a way to split the locals into those that are shared and those that are re-assigned for each new console 🤔

In the meantime, note that you can use a workaround like:

        from types import SimpleNamespace
        server = await aioconsole.server.start_console_server(
            host=host, port=port, locals={"shared": SimpleNamespace()}
        )

And then use the following in the console:

>>> shared.profile = cProfile.Profile()
>>> shared.profile.enable()
# Next connection
>>> shared.profile.disable()
>>> shared.profile.print_stats()

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
@pbabics pbabics force-pushed the feat/console-shared-locals branch from f29db3f to e2c633c Compare October 24, 2023 16:11
@vxgmichel vxgmichel force-pushed the main branch 3 times, most recently from 47702be to 49459e6 Compare December 9, 2023 17:31
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.

None yet

2 participants