Home > Backend Development > Python Tutorial > python任务调度实例分析

python任务调度实例分析

WBOY
Release: 2016-06-10 15:12:18
Original
1409 people have browsed it

本文实例讲述了python任务调度实现方法。分享给大家供大家参考。具体如下:

方法1:

import sched, time
import os
s = sched.scheduler(time.time, time.sleep)
#scheduler的两个参数用法复杂,可以不做任何更改
def playmusic(x):
  os.system(x)
def jobtodo():
   tmlist = [2011,8,11,22,15,0,0,0,0]
   x1=time.mktime(tmlist)
   x2=time.time()
   y=x1-x2
#计算任务到现在的时间长度
   s.enter(y, 1, playmusic, ('mplayer /home/c.mp3',))
#四个参数分别为:间隔事件、优先级(用于同时间到达的两个事件同时执行时定序)、被调用触发的函数,给他
#的参数(注意:一定要以tuple给如,如果只有一个参数就(xx,))
   s.run()
   print time.time()
jobtodo()
Copy after login

方法2:

import os
import time
from threading import Timer
def playmusic(x):
  os.system(x)
def jobtodo():
   tmlist = [2011,8,11,22,40,0,0,0,0]
   x1=time.mktime(tmlist)
   x2=time.time()
   y=x1-x2
   Timer(y, playmusic, ('mplayer /home/b.mp3',)).start()
jobtodo()
Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template