0

so I have a python script that make a POST call with some arguments (ID and Date), that means that at a specific date time makes a POST passing and ID.

Now what I want to achieve is having a script that puts jobs in the queue and another one that execute those script at that specific date time.

The script stays the same every time but it's date and ID that changes.

I can of course run multiple times the same script with different arguments and using time.sleep wait until it gets executed but I am trying to find a cleaner way...

jack87
  • 465
  • 2
  • 5
  • 13

1 Answers1

0

You can try to use sched module.
Here is an example that I made for myself in the past to deal with the discord:

import sched
import time
import sh_executor
import sh_logger as log
import sv_discord

s = None


def init():
    log.info("Initializing Scheduler...")
    global s
    s = sched.scheduler(time.time, time.sleep)
    sh_executor.submit_task(_start)


def stop():
    s.cancel(_restart_discord)


def _start():
    if sv_discord.DiscordSettings.IS_BOT_ENABLED:
        s.enter(30, 1, _restart_discord)
    s.run()


def _restart_discord():
    log.info("Scheduler: restarting Discord...")
    sv_discord.reconnect()
    s.enter(3600, 1, _restart_discord)

Also: Take a look here (python cron-like jobs)

IgorZ
  • 1,125
  • 3
  • 20
  • 44
  • Sorry can you provide more details on how would this work? I'm not getting it sorry... – jack87 Sep 05 '21 at 11:22
  • @jack87 you can find more details in the documentation https://docs.python.org/3/library/sched.html – IgorZ Sep 05 '21 at 11:25
  • Also if you need something like cron take a look on this lib: https://github.com/dbader/schedule – IgorZ Sep 05 '21 at 11:35