免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 9051 | 回复: 4

【求助】python定时执行任务 [复制链接]

论坛徽章:
1
丑牛
日期:2014-02-14 17:07:04
发表于 2012-08-15 17:29 |显示全部楼层
本帖最后由 ace_fei 于 2012-08-16 17:14 编辑
  1. #!/usr/bin/python
  2. import threading
  3. import time

  4. def func():
  5.     print "this func will run 2 min."
  6.     ...

  7. while 1:
  8.    timer=threading.Timer(0.025, func)
  9.    timer.start()
复制代码
我想每隔0.025秒创建一个新的线程执行func,如何控制这个时间?
上面这个方法不行, 它的时间间隔小于0.025秒。

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
发表于 2012-08-15 18:19 |显示全部楼层
  1. # -*- coding:gb2312 -*-

  2. import threading
  3. import time

  4. def thread_callback(total, interval):
  5.     for i in range(0, int(total / interval)):
  6.         print 'hello world'
  7.         time.sleep(interval)
  8.         
  9.    
  10. thread_handle = threading.Thread(None, thread_callback, kwargs = {'total':60, 'interval':0.025})
  11. thread_handle.start()
  12. #go on with your main thread
复制代码

论坛徽章:
13
双鱼座
日期:2013-10-23 09:30:05数据库技术版块每日发帖之星
日期:2016-04-20 06:20:00程序设计版块每日发帖之星
日期:2016-03-09 06:20:002015亚冠之塔什干火车头
日期:2015-11-02 10:07:452015亚冠之德黑兰石油
日期:2015-08-30 10:07:07数据库技术版块每日发帖之星
日期:2015-08-28 06:20:00数据库技术版块每日发帖之星
日期:2015-08-05 06:20:002015年迎新春徽章
日期:2015-03-04 09:57:09辰龙
日期:2014-12-03 14:45:52酉鸡
日期:2014-07-23 09:46:23亥猪
日期:2014-03-13 08:46:22金牛座
日期:2014-02-11 09:36:21
发表于 2012-08-16 08:46 |显示全部楼层
py的schedule也可以办这事

论坛徽章:
1
丑牛
日期:2014-02-14 17:07:04
发表于 2012-08-16 10:02 |显示全部楼层
回复 3# bikong0411
谢谢,我去查查。


   

论坛徽章:
1
丑牛
日期:2014-02-14 17:07:04
发表于 2012-08-16 16:44 |显示全部楼层
回复 2# linux_c_py_php


我现在要把print “hello world” 换成一个运行时间有2分钟的函数, 你这种方法就行不通了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP