Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在一个程序中实现多个信号?_Python_Timer_Alarms - Fatal编程技术网

Python 如何在一个程序中实现多个信号?

Python 如何在一个程序中实现多个信号?,python,timer,alarms,Python,Timer,Alarms,我开始学习Python(新手),所以对不同的模块没有太多的了解 我要模拟的场景: 我有一个程序prg1.py,我想运行一段用户定义的时间,比如t秒。在此时间(ts)后,程序应退出。为此,我使用signal.signal()创建报警。以下是工作代码: import signal import time import sys def receive_alarm(signum, stack): sys.exit('Exiting!') signal.

我开始学习Python(新手),所以对不同的模块没有太多的了解

我要模拟的场景:

我有一个程序
prg1.py
,我想运行一段用户定义的时间,比如
t
秒。在此时间(
t
s)后,程序应退出。为此,我使用
signal.signal()
创建报警。以下是工作代码:

import signal import time import sys def receive_alarm(signum, stack): sys.exit('Exiting!') signal.signal(signal.SIGALRM, receive_alarm) signal.alarm(10) while 1: print 'Working...' time.sleep(1) 这不行!简单退出,无任何错误或退出消息:(

如何实现此功能

注意:
线程的使用受到限制。


注意:
由于我希望程序继续侦听不同的信号,因此它无法睡眠,即无法使用time.sleep()。

您应该看看
sched
模块是否可以满足您的要求;另外,为什么不允许多线程

通过让警报以计划事件期间的最大公约数的时间间隔出现,可以使用单个警报计划多个任务。下面是一个示例,但它不是很可靠,例如,如果任何任务需要很长时间,则计时将不准确。这是可以修复的(使用
time.time()
,跟踪时间,并相应地安排下一个警报),但仍存在其他问题(如果一个任务运行时间过长,导致下一个任务启动延迟,会发生什么情况?).我的看法是,在许多棘手的情况下,调度程序是一个问题,如果可能的话,您应该使用现有的解决方案,而不是编写自己的解决方案

import signal
import time

class Scheduler(object):
    """Trivial scheduler object.

    Rather use sched.scheduler"""

    def __init__(self):
        self._tasks= [(1,self._heartbeat)]
        self._tick= 0
        self.stopped=False

    def addtask(self,period,task):
        """Add a task to be executed every PERIOD seconds

        addtask(period,task)

        period: seconds
        task: callable taking 'tick' argument
        """
        self._tasks.append( (period,task) )

    def _heartbeat(self,tick):
        print 'heartbeat: %d' % tick

    def _execute(self,signum,stack):
        if self.stopped:
            return

        self._tick += 1
        for period, task in self._tasks:
            if 0==self._tick % period:
                task(self._tick)
        signal.alarm(1)

    def start(self):
        signal.signal(signal.SIGALRM, self._execute)
        signal.alarm(1)

    def stop(self):
        self.stopped=True

class Stopper(object):
    """Callable to stop a scheduler"""
    def __init__(self, scheduler):
        self._scheduler=scheduler

    def __call__(self,tick):
        print 'stopping at tick',tick
        self._scheduler.stop()

def task3s(tick):
    print '3s task at tick',tick

def task7s(tick):
    print '7s task at tick',tick

s= Scheduler()
s.addtask(10,Stopper(s))
s.start()

s.addtask(3,task3s)
s.addtask(7,task7s)

while not s.stopped:
    time.sleep(0.5)
    print 'mainloop...'
在我的(python2)系统上,它提供:

mainloop...
heartbeat: 1
mainloop...
mainloop...
heartbeat: 2
mainloop...
mainloop...
heartbeat: 3
3s task at tick 3
mainloop...
mainloop...
heartbeat: 4
mainloop...
mainloop...
heartbeat: 5
mainloop...
mainloop...
heartbeat: 6
3s task at tick 6
mainloop...
mainloop...
heartbeat: 7
7s task at tick 7
mainloop...
mainloop...
heartbeat: 8
mainloop...
mainloop...
heartbeat: 9
3s task at tick 9
mainloop...
mainloop...
heartbeat: 10
stopping at tick 10
mainloop...

请注意,每个信号只能有一个处理程序。如果尝试设置多个处理程序,则只会调用最后一个处理程序。此外,
signal.alarm
仅支持一个报警。尝试设置两个报警,则只会取消前一个报警。如果要使用计时器,请使用
threading
模块中定义的
Timer
类。哦好的,但是我对线程的使用有限制:(也许你可以从这里得到一些提示
mainloop...
heartbeat: 1
mainloop...
mainloop...
heartbeat: 2
mainloop...
mainloop...
heartbeat: 3
3s task at tick 3
mainloop...
mainloop...
heartbeat: 4
mainloop...
mainloop...
heartbeat: 5
mainloop...
mainloop...
heartbeat: 6
3s task at tick 6
mainloop...
mainloop...
heartbeat: 7
7s task at tick 7
mainloop...
mainloop...
heartbeat: 8
mainloop...
mainloop...
heartbeat: 9
3s task at tick 9
mainloop...
mainloop...
heartbeat: 10
stopping at tick 10
mainloop...