以下是python脚本send_mms.py

############################################
#!/usr/bin/python
# -*- coding: utf-8 -*-

from twisted.application import service
from twisted.internet import reactor

def main(counter=0):
print counter, '...'
if counter == 10:
reactor.stop()
else:
reactor.callLater(60, main, counter+1) # 1分钟执行一次

if __name__ == "__main__":
reactor.callLater(1, main)
print 'Start!'
reactor.run()
print 'Stop!'

elif __name__=='__builtin__':
print '__builtin__'
reactor.callLater(1, main)
application=service.Application('send_mms')
############################################

shell 启动服务脚本send_mms.sh
############################################
#! /usr/bin/env sh
MAIN_MODULE=loop_mms
case $1 in
start)
PYTHONPATH=.:$PYTHONPATH twistd --python=$MAIN_MODULE.py --pidfile=/var/run/$MAIN_MODULE.pid --logfile=log/$MAIN_MODULE_client.log
;;
stop)
kill -9 `cat /var/run/$MAIN_MODULE.pid`
;;
restart)
kill -9 `cat /var/run/$MAIN_MODULE.pid`
sleep 1
PYTHONPATH=.:$PYTHONPATH twistd --python=$MAIN_MODULE.py --pidfile=/var/run/$MAIN_MODULE.pid --logfile=log/$MAIN_MODULE_client.log
;;
log)
tail -f log/$MAIN_MODULE_client.log
;;
*)
echo "Usage: ./$MAIN_MODULE.py start | stop | restart | log"
;;
esac
############################################

现在测试另一种情况,既然twisted 以callback的方法来执行我们的方法,你会想如果一个callback抛出了异常怎么办.让我们试试吧,basic-twisted/exception.py会在一个callback中抛出一个异常:

def falldown():

raise Exception('I fall down.')

def upagain():

print 'But I get up again.'

reactor.stop()

from twisted.internet import reactor

reactor.callWhenRunning(falldown)

reactor.callWhenRunning(upagain)

print 'Starting the reactor.'

reactor.run()

你会看到以下输出:

Starting the reactor.

Traceback (most recent call last):

... # I removed most of the traceback

exceptions.Exception: I fall down.

But I get up again.

注意第二个callback仍会在第一个callback之后运行,即使我们看到了很多的异常的追踪信息.如果你把reactor.stop()注释掉的话,这个程序会仍会继续运行下去,所以reactor 会继续运行下去即使我们的一个callback抛出了异常。

文章部分内容参考:http://floss.zoomquiet.io/data/20110709133658/index.html

python twisted启动定时服务的更多相关文章

  1. python twisted 的定时调用带参的函数

    无参情况:lc = task.LoopingCall(fun)如果fun带有参数,可以使用functools.partial传递 (fun2 = partial(fun, param1,[...]) ...

  2. Python循环定时服务功能(相似contrab)

    Python实现的循环定时服务功能.类似于Linux下的contrab功能.主要通过定时器实现. 注:Python中的threading.timer是基于线程实现的.每次定时事件产生时.回调完响应函数 ...

  3. Python 启动本地服务

    在 Linux 服务器上或安装了 Python 的机器上,Python自带了一个WEB服务器 SimpleHTTPServer,我们可以很简单的使用  python -m SimpleHTTPServ ...

  4. Python——Window启动服务

    一.新建启动服务程序 # ZPF # encoding=utf-8 import win32timezone from logging.handlers import TimedRotatingFil ...

  5. 【OF框架】使用OF.WinService项目,添加定时服务,进行创建启动停止删除服务操作

    准备 使用框架搭建完成项目,包含OF.WinService项目. 了解Window Service 和定时服务相关知识. 一.添加一个定时服务 第一步:了解项目结构 第二步:创建一个新的Job 第三步 ...

  6. 【转】Python Twisted介绍

    Python Twisted介绍 作者:Jessica McKellar 原文链接 Twisted是用Python实现的基于事件驱动的网络引擎框架.Twisted诞生于2000年初,在当时的网络游戏开 ...

  7. python实现编写windows服务

    使用python编写windows服务 最近测试服务器上经常发生磁盘空间不足,每次手动清除比较麻烦,所以写个windows服务定时清理下.中间也遇到过几个坑,一起记录下来. 1.python实现win ...

  8. Python Twisted系列教程16:Twisted 进程守护

    作者:dave@http://krondo.com/twisted-daemonologie/  译者: Cheng Luo 你可以从”第一部分 Twist理论基础“开始阅读:也可以从”Twisted ...

  9. 启动Mysql服务提示Can’t connect to local MySQL server through socket的解决方法

    启动Mysql服务常会提示下面错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/ ...

随机推荐

  1. 基数排序(radix sort)

    #include<iostream> #include<ctime> #include <stdio.h> #include<cstring> #inc ...

  2. select into from 提示 Undeclared variable.....错误的解决办法 && select into from 和 insert into select 的用法和区别

    然而今天在使用 SELECT INTO FROM 备份mysql数据表的时候,运行相关 sql 语句的时候却一直返回 [Err] 1327 - Undeclared variable: ...... ...

  3. 使yum保留下载的rpm包

    [root@14LN yum]# egrep 'cachedir|keepcache' /etc/yum.conf #cachedir=/var/cache/yum/$basearch/$releas ...

  4. TransactionScope使用说明

    TransactionScope是.Net Framework 2.0滞后,新增了一个名称空间.它的用途是为数据库访问提供了一个“轻量级”[区别于:SqlTransaction]的事物.使用之前必须添 ...

  5. UVALive 6884 GREAT + SWERC = PORTO dfs模拟

    题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  6. ember.js:使用笔记8 加载测试与集成测试

    emberjs使用的测试工具为qunit.js: 加载:将runner.js添加到Index.html:大致内容: if (window.location.search.indexOf("? ...

  7. 计算几何 2013年山东省赛 A Rescue The Princess

    题目传送门 /* 已知一向量为(x , y) 则将它旋转θ后的坐标为(x*cosθ- y * sinθ , y*cosθ + x * sinθ) 应用到本题,x变为(xb - xa), y变为(yb ...

  8. BZOJ4155 : [Ipsc2015]Humble Captains

    第一问最小割,第二问: 设du[i]表示i点的度数,则要最小化$\frac{|1集合的du[i]之和-2集合的du[i]之和|}{2}$, 压位01背包即可. #include<cstdio&g ...

  9. BZOJ3547 : [ONTAK2010]Matchings

    树形DP f[i][0]表示不向下连边的最大匹配数 f[i][1]表示向下连一条边的最大匹配数 h[][]表示对应的方案数 为了防止爆栈用BFS 为了防止MLE: 1.数组循环利用,比如存边的数组在存 ...

  10. CentOS6.4 配置HAProxy+Keepalived

    安装HAProxy请参考 http://www.cnblogs.com/kgdxpr/p/3272861.html 安装Keepalived 1.下载安装依赖包 yum install -y wget ...