'APScheduler AsyncIOScheduler() exits after script completion

I've read through a few of the Similar Questions provided, but I'm not able to get to a result that works for me. Likely can be chalked up to a general lack of understanding of these kinds of concepts from my limited Python experience.

I'm trying to call an async function on a schedule. The scheduler starts, but then the script exits. I thought this was going to run in a loop, keeping the script in the foreground but it seems that it doesn't.

Any pointers?

import asyncio
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import c_logging
import c_seed

client = 'async'
state = 'live'
account = 'spot'

async def main():
    c_logging.logging.info("")
    c,l = await c_seed.init(client,state,account)
    await c_seed.seed(c,l)
    scheduler = AsyncIOScheduler()
    scheduler.add_job(c_seed.seed, 'cron', minute='*', second='5', args=(c,l))
    scheduler.start()
    scheduler.print_jobs()

if __name__ == "__main__":
    c_logging.logging.info("")
    asyncio.run(main())
2021-07-02 10:11:24,204 [INFO] <module>: c_client
2021-07-02 10:11:24,205 [INFO] <module>: 
2021-07-02 10:11:24,205 [DEBUG] __init__: Using selector: KqueueSelector
2021-07-02 10:11:24,205 [INFO] main: 
2021-07-02 10:11:24,205 [INFO] init: 
2021-07-02 10:11:24,205 [INFO] create: c_client
2021-07-02 10:11:24,205 [INFO] create: c_client -> async
2021-07-02 10:11:24,205 [INFO] create: c_client -> live
2021-07-02 10:11:24,569 [INFO] pairs_list: 
2021-07-02 10:11:24,731 [DEBUG] seed: async seed start
2021-07-02 10:11:30,143 [DEBUG] seed: async seed complete
2021-07-02 10:11:30,218 [INFO] add_job: Adding job tentatively -- it will be properly scheduled when the scheduler starts
2021-07-02 10:11:30,219 [INFO] _real_add_job: Added job "seed" to job store "default"
2021-07-02 10:11:30,219 [INFO] start: Scheduler started
Jobstore default:
    seed (trigger: cron[minute='*', second='5'], next run at: 2021-07-02 10:12:05 EDT)
2021-07-02 10:11:30,219 [DEBUG] _process_jobs: Looking for jobs to run
2021-07-02 10:11:30,220 [DEBUG] _process_jobs: Next wakeup is due at 2021-07-02 10:12:05-04:00 (in 34.779986 seconds)
%


Solution 1:[1]

Only when the script is running, the job will works.Look https://apscheduler.readthedocs.io/en/stable/faq.html

Solution 2:[2]

I think you should keep this script running endlessly because there is a cron task.

Just try to change asyncio.run(main()) to asyncio.get_event_loop().run_forever()

Hope this could help you.

You could check the following reference:

https://github.com/agronholm/apscheduler/blob/3.x/examples/schedulers/asyncio_.py

Run a function every n seconds in python with asyncio

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2