Skip to main content

decorator for monitor function result, run callback when result has been changed

Project description

变更触发器

应用场景

周期性的执行某个任务, 将任务的结果存储于磁盘中。 当每次检测到结果和上次不同时, 更新外部存储的结果并触发回调任务

安装

pip install diff_trigger

使用

举个栗子

from diff_trigger import watchdiff
import requests
import logging

def trigger(old, new):
    logging.info('data change from {} to {}'.format(old, new))

@watchdiff(dbpath='/var/cache/pythonjob', callback=trigger, key='somekey')
def job(url):
    r = requests.get(url)
    return r.content

class SomeClass:
    @watchdiff(dbpath='/var/cache/pythonjob', callback=trigger)
    def job(self, url):
        r = requests.get(url)
        return r.content

说明

  • 定义一个有两个参数的回调函数,第一个参数会被传递为旧的结果,第二个参数会被传递为新的结果。根据实际情况使用这个数据
  • 使用watchdiff作为装饰器,填入本地的数据地址,以及所要指定的触发函数名。可以手工指定一个键,函数结果会存放在这个键下面。如果不指定键名,会根据函数的调用参数来生成一个键
  • 在类中使用时,整个函数的签名计算会有整个实例的序列化信息, 因此尽量不要在体积较大的类中使用。或者使用staticmethod
  • 如果在类中作为装饰器使用,需要注意的是实例的改变会更改本地数据库中的键值,导致无法触发回调函数。只有在实例不变的情况下才会有效。或者使用staticmethod

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

diff_trigger-1.1.0.tar.gz (3.0 kB view details)

Uploaded Source

File details

Details for the file diff_trigger-1.1.0.tar.gz.

File metadata

  • Download URL: diff_trigger-1.1.0.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.13.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for diff_trigger-1.1.0.tar.gz
Algorithm Hash digest
SHA256 2465e02f32d80cf65587099de280d99488c0228bc9132cdecc86c7d689897197
MD5 533966d4f333fdfb7d084faebb36080f
BLAKE2b-256 5e32c873f4afbd195bdf910e915a6542d81cb212f7318ad9cb5986538a8ab0a0

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