Withwait
A simple utility to ensure that sleep operations always complete, even if an exception happens within the with statement.
Usage
from withwait import wait
# Start a 2 second wait timer
with wait(2):
# These operations happen while the timer is run
print("Started timer")
# Even if an error is raised, the timer will always be allowed to complete
# before the withwait block is closed
raise Exception("Yikes")
# The exception isn't actually caught so this code won't run
print("This never prints")
Cancelling a timer
If you need to stop the timer during the operation, you can stop it using the
cancel method. Normal running will resume from the end of the with
statement.
from withwait import wait
with wait(1) as timer:
# Stop the timer
timer.cancel()
# Code after this point in the with statement won't be run
# Program flow resumes normally after
print("Ok")
- Different timers can be cancelled separately - if you cancel one timer, all other timers will run to completion.
Aborting a timer
If you don't want to resume normal program flow after the with statement,
you can stop the timer using the abort method.
from withwait import wait, WithwaitAbort
try:
with wait(1) as timer:
# Abort the timer
timer.abort()
# Code after this point in the with statement won't be run
# And code after the with statement won't be run either
print("Nope")
# We need to catch the exception ourselves
except WithwaitAbort:
print("Caught")
- Arguments can be provided to the
abortmethod which will be given as arguments to the exception - Different timers can be aborted separately - if you cancel one timer, all other timers will run to completion.
Aborting all timers
If you want to cancel anything, you can call the abort_all method. You can
also raise a WithwaitAbortAll exception, which will have the same effect.
from withwait import wait, WithwaitAbortAll
try:
with wait(1):
with wait(1):
with wait(1) as timer:
# Abort all timers
timer.abort_all()
except WithwaitAbortAll:
print("All timers stopped")
Release files for withwait 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| withwait-0.1.2.tar.gz | 3.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| withwait-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.7 kB
Release files / withwait-0.1.2.tar.gz
| Download URL | withwait-0.1.2.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e416faf3624294ce8e594239c600d5525044778f040b1251e99fd91f490452f4
|
|
BLAKE2b-256 checksum How to use checksums |
db57e62964d881374132d778465c165f30456e5241edfb1523a9106e839cd0c1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.9.19 Linux/6.5.0-1021-azure
|
Release files / withwait-0.1.2-py3-none-any.whl
| Download URL | withwait-0.1.2-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
82dac56f0f9d33ae7e3f1289640820c65d6364b6b644f8f8ec78319f558843e1
|
|
BLAKE2b-256 checksum How to use checksums |
385e754298fcfd37acdc364d0ac7b5bbbd25f24968189364b74d5559d3d3c9e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.9.19 Linux/6.5.0-1021-azure
|