#๐ Scheduling script
8 messages ยท Page 1 of 1 (latest)
@twilit glen
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
import asyncio
import time
import schedule
from datetime import datetime
# Define the asynchronous job function
async def my_async_job():
print(f"Async job started at {datetime.now()}")
await asyncio.sleep(2) # Simulate async work
print(f"Async job finished at {datetime.now()}")
# Define a function to run the async job
def run_async_job():
asyncio.run(my_async_job())
def main():
# Schedule the async job to run at 20:00 every day
schedule.every().hour.at(":56").do(run_async_job)
# Keep the script running
try:
print("Scheduler started. Waiting for jobs to run...")
while True:
schedule.run_pending()
except KeyboardInterrupt:
print("Shutting down scheduler...")
# Run the main function
if __name__ == "__main__":
main()
This doesnt work, nor this.....
import asyncio
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from datetime import datetime
from apscheduler.triggers.cron import CronTrigger
# Define the asynchronous job function
async def my_async_job():
print(f"Async job started at {datetime.now()}")
await asyncio.sleep(2) # Simulate async work
print(f"Async job finished at {datetime.now()}")
def main():
# Create an instance of the AsyncIOScheduler
scheduler = AsyncIOScheduler()
# Schedule the async job to run every 10 seconds
scheduler.add_job(my_async_job, CronTrigger(hour=20, minute=47))
# Start the scheduler
scheduler.start()
try:
# Keep the script running
asyncio.get_event_loop().run_forever()
except (KeyboardInterrupt, SystemExit):
# Shut down the scheduler on exit
print("Shutting down scheduler...")
scheduler.shutdown()
# Run the main function
if __name__ == "__main__":
main()
you could configure a cronjob, or whatever the equivalent is for your system. That is usually more reliable, and you don't need to worry about if your python script actually stays up.
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.