Andrew Cooke | Contents | Latest | RSS | Twitter | Previous | Next

C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

Personal Projects

Lepl parser for Python.

Colorless Green.

Photography around Santiago.

SVG experiment.

Professional Portfolio

Calibration of seismometers.

Data access via web services.

Cache rewrite.

Extending OpenSSH.

C-ORM: docs, API.

Last 100 entries

[Link, Programming] MySQL UTF8 Broken; NCOA for $20; [Link, Books] Latin American Authors; [Link, Computing] Optimizatin Puzzle; [Link, Books, Politics] Orwell Prize; [Link] What the Hell Is Happening With Qatar?; [Link] Deep Learning + Virtual Tensor Machines; [Link] Scaled Composites: Largest Wingspan Ever; [Link] SCP Foundation; [Bike] Lessons From 2 Leading 2 Trailing; [Link] Veg Restaurants in Santiago; [Link] List of Contemporary Latin American Authors; [Bike] FTHR; [Link] Whoa - NSA Reduces Collection (of US Residents); [Link] Red Bull's Breitbart; [Link] Linux Threads; [Link] Punycode; [Link] Bull / Girl Statues on Wall Street; [Link] Beautiful Chair Video; Update: Lower Pressures; [Link] Neat Python Exceptions; [Link] Fix for Windows 10 to Avoid Ads; [Link] Attacks on ZRTP; [Link] UK Jazz Invasion; [Review] Cuba; [Link] Aricle on Gender Reversal of US Presidential Debate; {OpenSuse] Fix for Network Offline in Updater Applet; [Link] Parkinson's Related to Gut Flora; Farellones Bike Park; [Meta] Tags; Update: Second Ride; Schwalbe Thunder Burt 2.1 v Continental X-King 2.4; Mountain Biking in Santiago; Books on Ethics; Security Fail from Command Driven Interface; Everything Old is New Again; Interesting Take on Trump's Lies; Chutney v6; References on Entropy; Amusing "Alexa.." broadcast; The Shame of Chile's Education System; Playing mp4 gifs in Firefox on Opensuses Leap 42.2; Concurrency at Microsoft; Globalisation: Uk -> Chile; OpenSuse 42.2 and Synaptics Touch-Pads; Even; Cherry Jam; Lebanese Writer Amin Maalouf; C++ - it's the language of the future; Learning From Trump; Chinese Writer Hu Fayun; And; Apricot Jam; Also; Excellent Article on USA Politics; Oh Metafilter; Prejudice Against The Rurals; Also, Zizek; Trump; Why Trump Won; Doxygen + Latex on CentOS 6; SMASH - Solve 5 Biggest Problems in Physics; Good article on racism, brexit, and social divides; Grandaddy are back!; Consciousness From Max Entropy; Democrats; Harvard Will Fix Black Poverty; Modelling Bicycle Wheels; Amusing Polling Outlier; If Labour keeps telling working class people...; Populism and Choice; Books on Defeat; Enrique Ferrari - Argentine Author; Transcript of German Scientists on Learning of Hiroshima; Calvert Journal; Owen Jones on Twitter; Possible Japanese Authors; Complex American Literature; Chutney v5; Weird Componentized Virus; Interesting Argentinian Author - Antonio Di Benedetto; Useful Thread on MetaPhysics; RAND on fighting online anarchy (2001); NSA Hacked; Very Good LRB Article on Brexit; Nussbaum on Anger; Tasting; Apple + Kiwi Jam; Hit Me; Sudoku - CSP + Chaos; Recycling Electronics In Santiago; Vector Displays in OpenGL; And Anti-Aliased; OpenGL - Render via Intermediate Texture; And Garmin Connect; Using Garmin Forerunner 230 With Linux; (Beating Dead Horse) StackOverflow; Current State of Justice in China; Axiom of Determinacy; Ewww; Fee Chaos Book

© 2006-2017 Andrew Cooke (site) / post authors (content).

Deadman timer for Google AppEngine

From: andrew cooke <andrew@...>

Date: Tue, 2 Aug 2011 20:49:35 -0400

class DeadmanTimer(Model):
    '''
    Each name is associated with a timestamp and a timeout period.  In normal
    use some process (whose continued existence is being monitored) will
    regularly call the `ping` method.  This will maintain the timestamp to
    within an accuracy of half the timeout period.  The `expired` method
    returns true if the timestamp is older than the timeout period.

    The uncertainty in the updating of the timestamp value allows us to
    provide an efficient (infrequent writing) implementation that accepts
    many pings within the given period.  So this is intended for use when
    the ping frequency is much higher than the checking for expiry.  For
    example, when a task is checked once an hour but will normally be running
    multiple times a second.
    '''

    timestamp = DateTimeProperty(required=True)
    timeout_period = IntegerProperty(required=True)

    def older_than(self, now, factor=1):
        age = now - self.timestamp
        secs = (age.microseconds + (age.seconds + age.days * 24 * 3600.0) *
        10**6) / 10**6
        return secs > factor * self.timeout_period

    @staticmethod
    def create(name, period):
        then = datetime.now() - timedelta(0, period+1)
        return DeadmanTimer.get_or_insert(name,
                        timeout_period=period, timestamp=then)

    @staticmethod
    def ping(name, default_period=60*60):
        logging.debug('ping %s' % name)
        timer = memcache.get('deadman:' + name)
        now = datetime.now()
        # if we may have expired then force a refresh
        if timer and timer.older_than(now, factor=0.5):
            timer = None
        if not timer:
            timer = DeadmanTimer.create(name, default_period)
            memcache.set('deadman:' + name, timer)
        if timer.older_than(now, factor=0.5):
            timer.timestamp = now
            timer.save()

    @staticmethod
    def expired(name, default_period=60*60):
        timer = DeadmanTimer.create(name, default_period)
        return timer.older_than(datetime.now())

Comment on this post