Tôi nghĩ rằng lỗi của bạn được mô tả tốt bởi bản sao mà tôi đã đánh dấu cũng như câu trả lời của @Jeff
Chỉnh sửa: Rõ ràng là không .. vì vậy ở đây tôi sẽ mô tả cách khắc phục sự cố phiên bản tối đa:
Vấn đề trường hợp tối đa
Khi bạn thêm công việc vào trình lập lịch, có một đối số, bạn có thể đặt cho số lượng các trường hợp đồng thời được phép tối đa của công việc. Bạn có thể đọc về điều này ở đây: BaseScheduler.Add_job []should read about this here: BaseScheduler.add_job[]
Vì vậy, việc khắc phục vấn đề của bạn chỉ là vấn đề đặt nó thành một cái gì đó cao hơn:
sch.add_job[myfn, 'interval', seconds=1, max_instances=10]
Nhưng, bạn muốn có bao nhiêu yêu cầu đồng thời? Nếu họ mất nhiều hơn một giây để trả lời và bạn yêu cầu một giây, cuối cùng bạn sẽ luôn gặp lỗi nếu bạn để nó chạy đủ lâu ...
Lập lịch
Có một số tùy chọn lịch trình có sẵn, đây là hai:
Nền tảng
Bạn đang nhập bộ lập lịch chặn - khối nào khi bắt đầu. Vì vậy, phần còn lại của mã của bạn không được thực thi cho đến khi bộ lập lịch dừng. Nếu bạn cần thực hiện mã khác sau khi bắt đầu trình lập lịch, tôi sẽ sử dụng bộ lập lịch nền như thế này:
from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
Chặnscheduler
Nếu bạn không cần thực hiện mã khác sau khi bắt đầu bộ lập lịch, bạn có thể sử dụng bộ lập lịch chặn và nó thậm chí còn dễ dàng hơn:
apscheduler.schedulers.blocking import BlockingScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
# Execute your code before starting the scheduler
print['Starting scheduler, ctrl-c to exit!']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
Trong bài viết này, chúng tôi sẽ thảo luận về cách thực hiện kịch bản Python sau mỗi 5 phút. Hãy để thảo luận về một số phương pháp để làm điều này.
Phương pháp 1: Sử dụng mô -đun thời gian
Chúng ta có thể tạo một tập lệnh Python sẽ được thực thi tại mọi thời điểm cụ thể. Chúng tôi sẽ vượt qua khoảng thời gian đã cho trong hàm.s ngủ [] và thực hiện trong khi vòng lặp là đúng. Chức năng sẽ ngủ trong khoảng thời gian nhất định. Sau đó, nó sẽ bắt đầu thực hiện.
Code:
Python3
import
time
while
[
True
from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
0from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
1from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
2[
from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
4from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
5from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
1from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
7from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
8from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
5Output:
Phương pháp 2: Sử dụng mô -đun lịch trình
Với sự trợ giúp của mô -đun lịch trình, chúng ta có thể tạo một tập lệnh Python sẽ được thực thi trong mỗi khoảng thời gian cụ thể nhất định. Với lịch trình này. Và với lịch trình trợ giúp.run_pending [], chúng tôi sẽ kiểm tra xem bộ lập lịch có chức năng đang chờ xử lý hay không.schedule.every[5].minutes.do[func] function will call every 5 minutes. And with the help schedule.run_pending[] we will check whether the scheduler has a pending function to run or not.
Code:
Python3
import
apscheduler.schedulers.blocking import BlockingScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
# Execute your code before starting the scheduler
print['Starting scheduler, ctrl-c to exit!']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
1import
time
Phương pháp 2: Sử dụng mô -đun lịch trình
from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
1from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
2[
apscheduler.schedulers.blocking import BlockingScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
# Execute your code before starting the scheduler
print['Starting scheduler, ctrl-c to exit!']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
9from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
5Với sự trợ giúp của mô -đun lịch trình, chúng ta có thể tạo một tập lệnh Python sẽ được thực thi trong mỗi khoảng thời gian cụ thể nhất định. Với lịch trình này. Và với lịch trình trợ giúp.run_pending [], chúng tôi sẽ kiểm tra xem bộ lập lịch có chức năng đang chờ xử lý hay không.
import
apscheduler.schedulers.blocking import BlockingScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
# Execute your code before starting the scheduler
print['Starting scheduler, ctrl-c to exit!']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
1from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
1crontab -e8
from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
1from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
7crontab -e2
from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
5Output:
4 apscheduler.schedulers.blocking import BlockingScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
# Execute your code before starting the scheduler
print['Starting scheduler, ctrl-c to exit!']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
5apscheduler.schedulers.blocking import BlockingScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
# Execute your code before starting the scheduler
print['Starting scheduler, ctrl-c to exit!']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
Tiện ích Cronjob là một bộ lập lịch công việc dựa trên thời gian trong các hệ điều hành giống UNIX. Cron cho phép người dùng Linux và Unix chạy các lệnh hoặc tập lệnh tại một thời gian và ngày nhất định. Một khi có thể lên lịch các tập lệnh được thực thi định kỳ.job utility is a time-based job scheduler in Unix-like operating systems. Cron allows Linux and Unix users to run commands or scripts at a given time and date. Once can schedule scripts to be executed periodically.
Dưới đây là chương trình mẫu để trình diễn:sample program for demonstration:
Python3
apscheduler.schedulers.blocking import BlockingScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
# Execute your code before starting the scheduler
print['Starting scheduler, ctrl-c to exit!']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
4 */5 * * * * /home/$[USER]/my_script.py4
from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
1from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
2[
*/5 * * * * /home/$[USER]/my_script.py8
from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
5import
0 import
1import
22____52 import
4
crontab -e6
from apscheduler.schedulers.background import BackgroundScheduler as scheduler
def myfn[]:
# Insert your requests code here
print['Hello']
sch = scheduler[]
sch.add_job[myfn, 'interval', seconds=5]
sch.start[]
# This code will be executed after the sceduler has started
try:
print['Scheduler started, ctrl-c to exit!']
while 1:
# Notice here that if you use "pass" you create an unthrottled loop
# try uncommenting "pass" vs "input[]" and watching your cpu usage.
# Another alternative would be to use a short sleep: time.sleep[.1]
#pass
#input[]
except KeyboardInterrupt:
if sch.state:
sch.shutdown[]
1import
7Biểu thức lập lịch Crontab có các phần sau:
Để lên lịch tập lệnh của chúng tôi để được thực thi, chúng tôi cần nhập biểu thức lập lịch Crontab vào tệp crontab.Để làm điều đó, chỉ cần nhập phần sau vào thiết bị đầu cuối:
crontab -e
Bạn có thể được nhắc chọn Trình chỉnh sửa, chọn Nano và nối dòng sau vào cuối tệp Crontab đã mở:
*/5 * * * * /home/$[USER]/my_script.py
Sau khi chạy tập lệnh các tập lệnh Python của chúng tôi được thực thi trong 5 phút một lần.