Skip to main content

Run a python script or function only once in a given time frame.

Project description

once_only

https://img.shields.io/pypi/v/once_only.svg Documentation Status

Run a python script or function only once in a given time frame.

If, for example, you have a script or service which might be called frequently, but you want to report errors only once daily to not annoy people too much, once_only is the library for you.

Quickstart

Suppose you want your script to complain, but not more than once a day. once_only.daily is the tool you need:

import once_only

@once_only.daily
def complain():
    print("This is not right!")

Now you can run complain as often as you want, from the same python script or from others, it will only be run at most once a day, so that at least 24 hours are between two complaints.

If you want to complain more or less often, there are other variants:

object

time delta

once_only.weekly

1 week

once_only.daily

1 day

once_only.hourly

1 hour

once_only.minutely

1 minute

once_only.Once()

custom datetime.timedelta

Advanced Usage

Instead of using a once_only.Once object as a decorator, you can also access it directly via the check_ready() and check_ready_trigger() functions:

import once_only
import datetime

once_every_two_hours = once_only.Once(datetime.timedelta(hours=2))

if once_every_two_hours.check_ready():
    print("More than two hours have passed since last run!")

if not_a_dry_run and once_every_two_hours.check_ready_trigger():
    print("Triggering timer and running!")

Note that all instances of once_only.Once with the same time delta share the same timer, but those with different time deltas don’t share the timer. So, if you have never run anything before, this:

import once_only
import datetime

@once_only.minutely
def run_minutely():
    print("minutely")

@once_only.hourly
def run_hourly():
    print("hourly")

@once_only.Once(datetime.timedelta(minutes=60))
def run_every_60_minutes():
    print("60 minutes")

run_minutely()
run_hourly()
run_every_60_minutes()

will print “minutely” and “hourly”, but not “60 minutes” because 60 minutes is the same as one hour, so the 60 minutes timer will already be triggered by run_hourly and run_every_60_minutes will not be run.

Further documentation can be found at: https://once_only.readthedocs.io.

License

Copyright 2022, Potsdam-Institut für Klimafolgenforschung e.V.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Changelog

1.0.0 (2022-09-16)

  • Initial commit.

  • once_only.Once class implemented, with direct access API and decorator functionalities.

  • Add Quickstart documentation.

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

once_only-1.0.0.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

once_only-1.0.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file once_only-1.0.0.tar.gz.

File metadata

  • Download URL: once_only-1.0.0.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for once_only-1.0.0.tar.gz
Algorithm Hash digest
SHA256 668be5d51d32df804880dd54ec08bfb707b68be11092e93fb46440647ac355f8
MD5 2368ec17cb71800060043755465997fe
BLAKE2b-256 c5b26c958a0a5737a2b92e16679f11d3e7f1831c482bf50adda6c36319dec914

See more details on using hashes here.

File details

Details for the file once_only-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: once_only-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for once_only-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a51578cdd39609dd63075881f0bca195418486f8300213617b1399e5b75f8389
MD5 388e154422600d1c43070cb7e7167409
BLAKE2b-256 bd2f62f266ef12e45c7748335c2b90dfc261862591d472b27726daac98558009

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