繁体   English   中英

python apscheduler,一种运行作业的简便方法?

[英]python apscheduler, an easier way to run jobs?

我有安排直通工作apscheduler 到目前为止,我有3个工作,但很快就会有更多工作。 我正在寻找一种扩展我的代码的方法。

当前,每个作业都是其自己的.py文件,在该文件中,我已将脚本转换为以run()作为函数名称的函数。 这是我的代码。

from apscheduler.scheduler import Scheduler
import logging

import job1
import job2
import job3

logging.basicConfig()
sched = Scheduler()

@sched.cron_schedule(day_of_week='mon-sun', hour=7)

def runjobs():
    job1.run()
    job2.run()
    job3.run()    

sched.start()

这行得通,现在代码只是愚蠢的,但是可以完成工作。 但是当我有50个工作时,代码会很愚蠢。 如何缩放?

注意:作业的实际名称是任意的,并且不遵循模式。 该文件的名称为scheduler.py ,我在python shell中使用execfile('scheduler.py')运行该execfile('scheduler.py')

import urllib
import threading
import datetime

pages = ['http://google.com', 'http://yahoo.com', 'http://msn.com']

#------------------------------------------------------------------------------
# Getting the pages WITHOUT threads
#------------------------------------------------------------------------------
def job(url):
    response = urllib.urlopen(url)
    html = response.read()

def runjobs():
    for page in pages:
        job(page)

start = datetime.datetime.now()
runjobs()
end = datetime.datetime.now()

print "jobs run in {} microseconds WITHOUT threads" \
      .format((end - start).microseconds)

#------------------------------------------------------------------------------
# Getting the pages WITH threads
#------------------------------------------------------------------------------
def job(url):
    response = urllib.urlopen(url)
    html = response.read()

def runjobs():
    threads = []
    for page in pages:
        t = threading.Thread(target=job, args=(page,))
        t.start()
        threads.append(t)

    for t in threads:
        t.join()

start = datetime.datetime.now()
runjobs()
end = datetime.datetime.now()

print "jobs run in {} microsecond WITH threads" \
      .format((end - start).microseconds)

看@

http://furius.ca/pubcode/pub/conf/bin/python-recursive-import-test

这将帮助您导入所有python / .py文件。

例如,在导入时,您可以创建一个列表,其中保留了一个函数调用。

[job1.run(),job2.run()]

然后遍历它们并调用函数:)

感谢Arjun

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM