Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.8k views
in Technique[技术] by (71.8m points)

python - How to manage a task queue using APScheduler?

I would like to use APScheduler to handle task scheduling and queues. I am currently using the very basic APScheduler settings: BackgroundScheduler() I know how to start the scheduler and set some cron on interval trigger job. But I can't figure out how to handle a proper queue.

Here is my code:

from apscheduler.schedulers.background import BackgroundScheduler


scheduler = BackgroundScheduler()

def test():
    print("Hello World !")

def start_job():
    scheduler.add_job(
        test
    )

start_job() # first task
start_job() # second task
start_job() # third task

scheduler.start()

This code kind of works but I need my tasks to run one after one and not all at the same time.

I've tried using the max_instances attribute but it doesn't change anything.

I've also tried to add an id to my job and then only the first task is started but not the left ones giving these warning: Execution of job "test (trigger: date[2021-01-12 17:55:48 UTC], next run at: 2021-01-12 17:55:48 UTC)" skipped: maximum number of running instances reached (1)

How am I supposed to start the left tasks ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The executor you choose determines how the jobs are executed – whether in parallel or in a sequence. If you don't want any parallelism, you can use a ThreadPoolExecutor with max_workers=1 to run them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...