English 中文(简体)
Python Run Two Functions With Different Timers In One Daemon
原标题:

I m using the template python daemon discussed here in two different scripts to launch two separate daemons. I would like to combine them into one daemon script that has one lockfile, etc. However, each has different loop timer, one at 1 minute and the other at 5 minutes. I m starting with this:

import os
import subprocess
import time
from daemon import runner

class App():
    def __init__(self):
        self.stdin_path =  /dev/null 
        self.stdout_path =  /dev/tty 
        self.stderr_path =  /dev/tty 
        self.pidfile_path =   /tmp/test.pid 
        self.pidfile_timeout = 5
    def run(self):

        try:
            while True:

                print "hello!"

                # set the sleep time for repeated action here:
                time.sleep(1)

        except Exception, e:
            raise


app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

The obvious thing to do would be to create another class but I want the basic stuff like pidfile to remain constant.

最佳回答

I m don t entirely get why you want to do this, but this should work:

Make run_A and run_B functions to do the two independent things (obviously not those names), including the sleeps and so on. It doesn t really matter, but it might make sense to have these outside the App class.

Then make a run function that looks like this:

def run(self):
    threads = [
         threading.Thread(target=run_A),
         threading.Thread(target=run_B)
    ]

    for thread in threads:
         thread.start()
    for thread in threads:
         thread.join()

This will spawn a separate thread for each job, which can then go on its merry way. If you have any shared resources between the jobs, you ll have to worry about locking.

This assumes that the jobs are quick to execute and exact timings don t really matter; because of CPython s GIL, only one thread will run Python statements at once, so if they pop up at the same time they ll run a bit slower. You could pretty trivially switch this to use multiprocessing if you want them to be able to run in parallel.

This will result in some "timer drift" because you re using sleep (as would your daemons if run independently). If this matters, you could use interrupts instead, or calculate the time until the next multiple of five minutes rather than just sleeping five minutes.

问题回答

If it isn t essential for your longer delay to execute exactly on time, you can use a simple accumulator to keep track of how much time has been used by several cycles of the shorter action. The code below, will work perfectly if LONG_DELAY is an exact integer multiple of SHORT_DELAY. If it is not an exact multiple, the longer cycle will still execute with the requested period on average, but the delay may be shorter or longer by up to SHORT_DELAY on any given pass:

SHORT_DELAY = 60 # 1 minute
LONG_DELAY = 300 # 5 minutes

accum = LONG_DELAY

while True:
    do_short_delay_action()

    if accum >= LONG_DELAY:
        do_long_delay_action()
        accum -= LONG_DELAY

    time.sleep(SHORT_DELAY)
    accum += SHORT_DELAY




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签