我使用了这个框架的 django-apscheduler
代码内容
# -*- coding: utf-8 -*-
import datetime
import time
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job
from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor
executors = {
'default': ThreadPoolExecutor(10),
'processpool': ProcessPoolExecutor(3)
}
scheduler = BackgroundScheduler(executors=executors)
scheduler.add_jobstore(DjangoJobStore(), 'default')
def cron_test():
print '当前时间:{0}'.format(datetime.datetime.now())
def cron_task(run_time):
task_idss = str(time.time())
print task_idss
scheduler.add_job(func=cron_test, next_run_time=run_time, id=task_idss)
scheduler.start()
在 django 的一个 view 里面调用
class GameOpenTimeView(View):
def post(self, request, obj_id):
print obj_id
open_time = request.POST.get("open_time")
print open_time
open_time = datetime.datetime.strptime(open_time,"%Y-%m-%d %H:%M:00")
print type(open_time)
print open_time
cron_task(open_time)
return JsonResponse({'status': 'success'})
时间由前端页面传入
问题
当我第一次传一个时间进入,可以正常生成一个任务点
当我第二次传入一个时间,出现告警说
return self.dispatch(request, *args, **kwargs)
File "/Users/apple/OneDrive/Code_7zGame/Envs/tdops/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/Users/apple/OneDrive/vultr_svn/Django_myself/tdops/pro1/views.py", line 340, in post
cron_task(open_time)
File "/Users/apple/OneDrive/vultr_svn/Django_myself/tdops/opsbase/schedwork.py", line 23, in cron_task
scheduler.start()
File "/Users/apple/OneDrive/Code_7zGame/Envs/tdops/lib/python2.7/site-packages/apscheduler/schedulers/background.py", line 33, in start
BaseScheduler.start(self, *args, **kwargs)
File "/Users/apple/OneDrive/Code_7zGame/Envs/tdops/lib/python2.7/site-packages/apscheduler/schedulers/base.py", line 134, in start
raise SchedulerAlreadyRunningError
SchedulerAlreadyRunningError: Scheduler is already running
[04/Sep/2018 20:53:21] "POST /pro1/gameopentime/1003/ HTTP/1.1" 500 18550
这个是为何,这个模块之前也是有用过,也没出现这样当情况,不过之前是直接使用了 apscheduler 模块 然后后面也有换成了 apscheduler 模块试过,也是一样情况的
正常不是一个任务就一个 scheduler.start() 的么,为何不给再起一个的。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.