Python 设置运行进程的计时器,在特定条件下暂停计时器
我有一个程序:Python 设置运行进程的计时器,在特定条件下暂停计时器,python,timer,multiprocess,Python,Timer,Multiprocess,我有一个程序: import multiprocessing import time def timer(sleepTime): time.sleep(sleepTime) fooProcess.terminate() fooProcess.join() #line said to "cleanup", not sure if it is required, refer to goo.gl/Qes6KX def foo(): i=0 while 1
import multiprocessing
import time
def timer(sleepTime):
time.sleep(sleepTime)
fooProcess.terminate()
fooProcess.join() #line said to "cleanup", not sure if it is required, refer to goo.gl/Qes6KX
def foo():
i=0
while 1
print i
time.sleep(1)
i
if i==4:
#pause timerProcess for X seconds
fooProcess = multiprocessing.Process(target=foo, name="Foo", args=())
timer()
fooProcess.start()
正如您在注释中所看到的,在某些条件下(在本例中,我必须是4),计时器必须停止某个X时间,而foo()则继续工作。
现在,我如何实现这一点
注意:这段代码只是一个例子,重点是我想在特定条件下暂停进程一段时间。大致如下:
下面是一个示例,我如何理解,您的程序应该做什么: import threading, time, datetime ACTIVE = True def main(): while ACTIVE: print "im working" time.sleep(.3) def run(thread, timeout): global ACTIVE thread.start() time.sleep(timeout) ACTIVE = False thread.join() proc = threading.Thread(target = main) print datetime.datetime.now() run(proc, 2) # run for 2 seconds print datetime.datetime.now() 导入线程、时间、日期时间 活动=真 def main(): 激活时: 打印“我在工作” 时间。睡眠(.3) def运行(线程,超时): 全球活跃 thread.start() 时间。睡眠(超时) 活动=错误 thread.join() proc=threading.Thread(目标=main) 打印datetime.datetime.now() 运行(程序,2)#运行2秒 打印datetime.datetime.now() 在main()中,它执行周期性任务,这里是打印内容。在run()方法中,您可以说main应该执行任务多长时间 此代码产生以下输出: 2014-05-25 17:10:54.390000 im working im working im working im working im working im working im working 2014-05-25 17:10:56.495000 2014-05-25 17:10:54.390000 我在工作 我在工作 我在工作 我在工作 我在工作 我在工作 我在工作 2014-05-25 17:10:56.495000
如果我理解错了,请纠正我。我将使用
多处理.Pipe
来发送信号,并结合使用以下方法来计时:
#!/usr/bin/env python
import multiprocessing
import select
import time
def timer(sleeptime,pipe):
start = time.time()
while time.time() < start + sleeptime:
n = select.select([pipe],[],[],1) # sleep in 1s intervals
for conn in n[0]:
val = conn.recv()
print 'got',val
start += float(val)
def foo(pipe):
i = 0
while True:
print i
i += 1
time.sleep(1)
if i%7 == 0:
pipe.send(5)
if __name__ == '__main__':
mainpipe,foopipe = multiprocessing.Pipe()
fooProcess = multiprocessing.Process(target=foo,name="Foo",args=(foopipe,))
fooProcess.start()
timer(10,mainpipe)
fooProcess.terminate()
# since we terminated, mainpipe and foopipe are corrupt
del mainpipe, foopipe
# ...
print 'Done'
#/usr/bin/env python
导入多处理
导入选择
导入时间
def定时器(睡眠时间、管道):
开始=时间。时间()
while time.time()
我假设您需要
foo
过程中的某些条件来扩展计时器。在我设置的示例中,每当foo
达到7的倍数时,它会将计时器延长5秒,而计时器最初会倒计时10秒。在计时器结束时,我们终止进程-foo
将无法很好地完成,管道将被损坏,但您可以确定它将死亡。否则,你可以沿着主管道发回一个信号,foo
可以在你加入时很好地监听并退出。我认为你在游戏设计上犯了这个错误。游戏总是(没有例外)使用软件控制的主要事件循环
每次通过循环,您都会检查时间并根据经过的时间触发所有必要的事件。在循环结束时,在下一个计时器、事件、刷新、ai检查或其他状态更改之前,您只需在必要时睡眠
这使您在延迟、一致性、可预测性和其他游戏中重要的计时功能方面获得了最佳性能。您能从更高的层面告诉我们您想要实现的目标吗?也就是说,为什么您需要这个计时器,以及到底是什么驱动了您在这里使用的语义?非常简单,我正在编写一个程序(实际上是我正在处理的项目的CLI小游戏),它在特定条件下会触发计时器。当定时器处于活动状态时,程序必须正常运行,但当时间用完时,整个程序必须停止。通过激活特定功能,计时器必须暂停X秒。这也可以通过延长计时器的时间来实现,但我认为这个间隔更为棘手。谢谢,这应该会起作用!只要给我时间,让我对你的代码充满信心,并将其实现到我的代码中。OMG,这肯定是最简单、更有效的方法!非常感谢D