Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
LAE
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
文靖昊
LAE
Commits
7891dc93
Commit
7891dc93
authored
a year ago
by
文靖昊
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
调度器实现,用来执行定时任务
parent
9ad4e078
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
0 deletions
+37
-0
scheduler.py
src/scheduler/scheduler.py
+37
-0
No files found.
src/scheduler/scheduler.py
0 → 100644
View file @
7891dc93
import
datetime
from
apscheduler.schedulers.background
import
BackgroundScheduler
class
TaskScheduler
:
def
__init__
(
self
):
self
.
scheduler
=
BackgroundScheduler
()
def
add_task
(
self
,
timedTask
,
timeMinute
):
# Define your timed task here
self
.
scheduler
.
add_job
(
timedTask
,
'interval'
,
minutes
=
timeMinute
)
def
start_scheduler
(
self
):
# Add the timed task to the scheduler
# Start the scheduler
self
.
scheduler
.
start
()
def
stop_scheduler
(
self
):
# Shutdown the scheduler
self
.
scheduler
.
shutdown
()
def
task1
():
print
(
datetime
.
datetime
.
utcnow
()
.
strftime
(
"
%
Y-
%
m-
%
d
%
H:
%
M:
%
S.
%
f"
)[:
-
3
])
def
task2
():
print
(
"task2"
)
# Example usage:
if
__name__
==
"__main__"
:
scheduler
=
TaskScheduler
()
scheduler
.
add_task
(
task1
,
1
)
scheduler
.
add_task
(
task2
,
2
)
scheduler
.
start_scheduler
()
# Keep the program running to allow scheduler to execute tasks
try
:
while
True
:
pass
except
KeyboardInterrupt
:
scheduler
.
stop_scheduler
()
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment