2011-05-23 70 views
10

说,如果我有如下四个功能:的Python:交流功能每隔x分钟

def foo(): 
    subprocess.Popen('start /B someprogramA.exe', shell=True) 

def bar(): 
    subprocess.Popen('start /B someprogramB.exe', shell=True) 

def foo_kill(): 
    subprocess.Popen('taskkill /IM someprogramA.exe') 

def bar_kill(): 
    subprocess.Popen('taskkill /IM someprogramB.exe') 

我怎么能替代foo和bar功能,每运行,比如30分钟? 含义:第一个30分钟 - 运行foo,第二个30分钟 - 运行bar,第三个30分钟 - 运行foo,依此类推。每次新的运行应该'杀死'前面的线程/ func。

我有一个倒计时器线程,但不知道如何'交替'的功能。

class Timer(threading.Thread): 
    def __init__(self, minutes): 
     self.runTime = minutes 
     threading.Thread.__init__(self) 


class CountDownTimer(Timer): 
    def run(self): 
     counter = self.runTime 
     for sec in range(self.runTime): 
      #do something   
      time.sleep(60) #editted from 1800 to 60 - sleeps for a minute 
      counter -= 1 

timeout=30 
c=CountDownTimer(timeout) 
c.start() 

编辑:我和尼古拉斯骑士的输入解决方案...

import threading 
import subprocess 
import time 

timeout=2 #alternate delay gap in minutes 

def foo(): 
    subprocess.Popen('start /B notepad.exe', shell=True) 

def bar(): 
    subprocess.Popen('start /B calc.exe', shell=True) 

def foo_kill(): 
    subprocess.Popen('taskkill /IM notepad.exe') 

def bar_kill(): 
    subprocess.Popen('taskkill /IM calc.exe') 


class Alternator(threading.Thread): 
    def __init__(self, timeout): 
     self.delay_mins = timeout 
     self.functions = [(foo, foo_kill), (bar, bar_kill)] 
     threading.Thread.__init__(self) 

    def run(self): 
     while True: 
      for f, kf in self.functions: 
       f() 
       time.sleep(self.delay_mins*60) 
       kf() 

a=Alternator(timeout) 
a.start() 

工作正常。

回答

6

你太过于复杂了。

while True: 
    foo() 
    time.sleep(1800) 
    foo_kill() 
    bar() 
    time.sleep(1800) 
    bar_kill() 

或者,如果你想以后可以轻松添加更多的功能:

functions = [(foo, foo_kill), (bar, bar_kill), ] # Just append more as needed 
while True: 
    for f, kf in functions: 
     f() 
     time.sleep(1800) 
     kf() 
+0

在线程外运行会限制其他代码执行? – siva 2011-05-24 03:06:36

+0

@siva:为了清楚起见,我不打算“不要在一个单独的线程中运行它”,我的意思是说你太过复杂了。将它包装在一个函数中,然后像你所做的那样在一个线程中启动它就好了。 – 2011-05-24 19:52:50

2

使用一个变量来记录您上次运行哪个函数。当定时器启动时,运行其他功能并更新变量。

10

请记住,函数是Python中的第一类对象。这意味着你可以将它们存储在变量和容器中!一种方法是:

funcs = [(foo, foo_kill), (bar, bar_kill)] 

def run(self): 
    counter = self.runTime 
    for sec in range(self.runTime): 
     runner, killer = funcs[counter % 2] # the index alternates between 0 and 1 
     runner() # do something 
     time.sleep(1800) 
     killer() # kill something 
     counter -= 1 
+0

有趣..会试试这个。 – siva 2011-05-24 03:07:05

1
import itertools, time 

# make sure the function are in the order you want to run them in 
# and grouped by the ones that you want to run together 
funcs = ((bar_kill, foo), (foo_kill, foo)) 

for func_killer, func in itertools.cycle(funcs) 
    func_killer() 
    func() 
    time.sleep(30 * 60) # pause for 30 minutes 

功能可以存储在列表中的蟒蛇,你可以使用for循环迭代他们。

itertools是一个模块来操纵迭代如列表。在这里,我们使用cycle来创建一个infinit循环,它将一次又一次地处理列表funcs中的函数。

+0

谢谢..在循环子模块上有很好的输入。 – siva 2011-05-25 09:12:46