Skip to main content

Facilitates exit of a Python CLI program in a controlled way

Project description

pressenter2exit (Press Enter To Exit)

pressenter2exit (“Press Enter To Exit”) facilitates long-running CLI programs to exit in clean and controlled way.

Documentation: pressenter2exit.readthedocs.io.

The Problem

Long running CLI (command line interface) programs can be useful, but there is often a need to exit them in a clean manner (as opposed to, for example, pressing ctrl-c).

Examples

  • Programs that ‘crunch’ on a large number of input data sets, but you want to be able to cleanly interrupt the program after it’s done with the current data set.

  • Programs that run in an ‘infinite loop’ waiting for some sort of input data to appear. You’d like to be able to exit the ‘infinite loop’ in a controlled way.

  • Long-running system tests where you want to run, say, overnight but when you return you want to exit in a controlled way so you can get proper status and statistics (as opposed to ctrl-c).

The benefit to exiting in a controlled way is that you don’t end up with a data set partially processed, in an unknown state, and/or be unable to output statistics from the run. By using pressenter2exit, processing that has been done so far can be useful and not merely thrown away, which can happen if a program is forcefully and immediately aborted.

Example Code

from time import time

from pressenter2exit import PressEnter2Exit

exit_control = PressEnter2Exit()

start_time = time()
# loops until enter is pressed or we reach the end of our run time
while exit_control.is_alive() and time() - start_time < 20.0:
    print("I've been waiting for %f seconds." % (time() - start_time))
    exit_control.join(4.0)  # use join() instead of time.sleep() to ensure an immediate exit
print('Done! Exiting after %f seconds.' % (time() - start_time))

Output when enter is pressed mid-run:

Press enter to exit:
I've been waiting for 0.000005 seconds.
I've been waiting for 4.000108 seconds.
Done! Exiting after 5.331482 seconds.

Output when the program is allowed to finish on its own:

Press enter to exit:
I've been waiting for 0.000006 seconds.
I've been waiting for 4.004918 seconds.
I've been waiting for 8.007299 seconds.
I've been waiting for 12.009813 seconds.
I've been waiting for 16.012574 seconds.
Done! Exiting after 20.016182 seconds.

Platform Support

pressenter2exit doesn’t use any platform specific libraries, so it should run on any platform that supports the CLI (e.g. Windows, MacOS, Linux, etc.).

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

pressenter2exit-0.2.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pressenter2exit-0.2.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pressenter2exit-0.2.1.tar.gz.

File metadata

  • Download URL: pressenter2exit-0.2.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for pressenter2exit-0.2.1.tar.gz
Algorithm Hash digest
SHA256 13b5be7c9e2320fee3bbead47582835a688eb19b1522544437181112e23e3353
MD5 5f7468ce1e24aa39bb35b9ed6f79d280
BLAKE2b-256 c61e434b45c2a157f64edf01e6902ce107f02a05f6743516e46676c080a3b389

See more details on using hashes here.

File details

Details for the file pressenter2exit-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pressenter2exit-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for pressenter2exit-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c2d8d25780023c035dda50cdc00bf69a7e8a308c0e9ab7315cc34070315bf0a2
MD5 5dc707f3dbcc40276a46d078ecd9c51f
BLAKE2b-256 d7ac73f83e101eb456b9e5fc948720ca759b9b4f8314adc40a748ff106dc983e

See more details on using hashes here.

Supported by

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