Skip to main content

Python module to provide a manhole in asyncio applications

Project description

aiomanhole

Manhole for accessing asyncio applications. This is useful for debugging application state in situations where you have access to the process, but need to access internal application state.

Adding a manhole to your application is simple:

from aiomanhole import start_manhole

start_manhole(namespace={
    'gizmo': application_state_gizmo,
    'whatsit': application_state_whatsit,
})

Quick example, in one shell, run this:

$ python -m aiomanhole

In a secondary shell, run this:

$ nc -U /var/tmp/testing.manhole
Well this is neat
>>> f = 5 + 5
>>> f
10
>>> import os
>>> os.getpid()
4238
>>> import sys
>>> sys.exit(0)

And you’ll see the manhole you started has exited.

The package provides both a threaded and non-threaded interpreter, and allows you to share the namespace between clients if you want.

I’m getting “Address is already in use” when I start! Help!

Unlike regular TCP/UDP sockets, UNIX domain sockets are entries in the filesystem. When your process shuts down, the UNIX socket that is created is not cleaned up. What this means is that when your application starts up again, it will attempt to bind a UNIX socket to that path again and fail, as it is already present (it’s “already in use”).

The standard approach to working with UNIX sockets is to delete them before you try to bind to it again, for example:

import os
try:
    os.unlink('/path/to/my.manhole')
except FileNotFoundError:
    pass
start_manhole('/path/to/my.manhole')

You may be tempted to try and clean up the socket on shutdown, but don’t. What if your application crashes? What if your computer loses power? There are lots of things that can go wrong, and hoping the previous run was successful, while admirably positive, is not something you can do.

Can I specify what is available in the manhole?

Yes! When you call start_manhole, just pass along a dictionary of what you want to provide as the namespace parameter:

from aiomanhole import start_manhole

start_manhole(namespace={
    'gizmo': application_state_gizmo,
    'whatsit': application_state_whatsit,
    'None': 5,  # don't do this though
})

When should I use threaded=True?

Specifying threaded=True means that statements in the interactive session are executed in a thread, as opposed to executing them in the event loop.

Say for example you did this in a non-threaded interactive session:

>>> while True:
...  pass
...

You’ve just broken your application! You can’t abort that without restarting the application. If however you ran that in a threaded application, you’d ‘only’ have a thread trashing the CPU, slowing down your application, as opposed to making it totally unresponsive.

By default, a threaded interpreter will time out commands after 5 seconds, though this is configurable. Not that this will not kill the thread, but allow you to keep running commands.

Change History

0.7.0 (23rd January 2022)
  • Added support for Python 3.10. Thank you to Peter Bábics for contributing this!

  • Removed support for Python 3.5.

0.6.0 (30th April 2019)
  • Don’t use the global loop. Thanks Timothy Fitz!

  • Allow a port of 0. Thanks Timothy Fitz!

  • Fix unit test failure.

0.5.0 (6th August 2018)
  • Fix syntax error in 3.7

  • Drop 3.4 support.

0.4.2 (3rd March 2017)
  • Handle clients putting the socket into a half-closed state when an EOF occurs.

0.4.1 (3rd March 2017)
  • Ensure prompts are bytes, broken in 0.4.0.

0.4.0 (3rd March 2017)
  • Ensure actual syntax errors get reported to the client.

0.3.0 (23rd August 2016)
  • Behaviour change aiomanhole no longer attempts to remove the UNIX socket on shutdown. This was flakey behaviour and does not match best practice (i.e. removing the UNIX socket on startup before you start your server). As a result, errors creating the manhole will now be logged instead of silently failing.

  • start_manhole now returns a Future that you can wait on.

  • Giving a loop to start_manhole now works more reliably. This won’t matter for most people.

  • Feels “snappier”

0.2.1 (14th September 2014)
  • Handle a banner of None.

  • Fixed small typo in MANIFEST.in for the changelog.

  • Feels “snappier”

0.2.0 (25th June 2014)
  • Handle multiline statements much better.

  • setup.py pointed to wrong domain for project URL

  • Removed pointless insertion of ‘_’ into the namespace.

  • Added lots of tests.

  • Feels “snappier”

0.1.1 (19th June 2014)
  • Use setuptools as a fallback when installing.

  • Feels “snappier”

0.1 (19th June 2014)
  • Initial release

  • Feels “snappier”

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiomanhole-0.7.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

aiomanhole-0.7.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file aiomanhole-0.7.0.tar.gz.

File metadata

  • Download URL: aiomanhole-0.7.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for aiomanhole-0.7.0.tar.gz
Algorithm Hash digest
SHA256 bf22b6ca4190cb35c9cdc94e4be31190353fdc320e1b346c032d0d47fc3ca453
MD5 4ffc820a8483ea7abae95408ce9c89f9
BLAKE2b-256 45dce102e0392da4304caa85e610df8aedd3ebeae0e67a29692b179233afef3f

See more details on using hashes here.

File details

Details for the file aiomanhole-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: aiomanhole-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for aiomanhole-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 17fa447202df21df26262bfae8705ada83f14abe78dc03b61ba649e2521ebe71
MD5 97f28aaafea5061d277b2926ac9b849f
BLAKE2b-256 e02c67d5acdcafc0f762effa8a9b258d1b17193ee70171580ea2d87f04d11833

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page