问题
I am trying to figure out whether APScheduler is the tool for my project's needs.
I can add a job.
I can get a list of jobs -- but I am having trouble interpreting the results. I want to have my program translate this returned job list into a format needed by other parts of the program. I need to extract the trigger parameters.
Let's say I'm using a cron trigger. I'll make a simple one for now:
new_job = self.scheduler.add_job(jobfunc,'cron', minute='*/5', args=["minute"]
Later, I want to get the jobs and then extract the basic information (that this is a cron trigger, that it is scheduled for 'minute'=*/5) -- basically I want to reconstruct the parameters with which I created the trigger.
I have looked at the jobs returned in the debugger, and I see that each job has a "trigger" member which has within it the information I need -- but I am not sure how to access it programmatically.
I would like to do something like this:
jobs=self.scheduler.get_jobs()
schedules=[]
for job in jobs:
schedules.append(<do something with job.trigger.fields>)
Where the element appended to schedules would look something like:
{'minute':'*/5'}
The documentation says:
To get a machine processable list of the scheduled jobs, you can use the get_jobs()
The question is, how should my 'machine' process this list?
回答1:
OK, thanks to a previously asked question on the APScheduler Google group, I have a solution!
The details have to be accessed through the fields, as I thought. The key to get the information from the individual field is to convert the field value to a string.
Here is an example which works:
schedules = []
for job in self.scheduler.get_jobs():
jobdict = {}
for f in job.trigger.fields:
curval = str(f)
jobdict[f.name] = curval
schedules.append(jobdict)
For a schedule with one job added as:
new_job = self.scheduler.add_job(jobfunc,'cron', second='*/5', args=["second"])
The resulting list comes out like this:
[{'week': '*', 'hour': '*', 'day_of_week': '*', 'month': '*', 'second': '*/5', 'year': '*', 'day': '*', 'minute': '*'}]
回答2:
APScheduler .get_jobs()
method returns a list of job instances.
For example, you can print information about all currently scheduled jobs with something like the following:
for job in scheduler.get_jobs():
print("name: %s, trigger: %s, next run: %s, handler: %s" % (
job.name, job.trigger, job.next_run_time, job.func))
Note that job names can repeat across different jobs.
You can find here the API reference for the job class. It doesn't explicitly list the job class members, but they match the variables in kwargs.
来源:https://stackoverflow.com/questions/45400456/how-to-interpret-list-of-jobs-returned-from-get-jobs-in-apscheduler