Python Forum

Full Version: How to convert Schedule to APScheduler module?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm a python newbie and need a little help with my code.

I need to convert my code to use APSCHEDULER and not SCHEDULE module because I need to use hours, minutes, seconds which SCHEDULE is not capable of. How can I convert this script to use hh:mm:ss ?

Note: I did read the apscheduler documentation and examples, and oddly enough there isn't anything about scheduling a job to run at exactly 7:45:30 PM. All the examples appeared to be geared towards cron jobs running every seconds=3.

Here is my code for schedule-module
import subprocess
import time
import schedule


def job1():

    subprocess.call("netsh interface portproxy add v4tov4 listenaddress=192.168.0.153 listenport=1101 connectaddress=192.168.0.153 connectport=809 protocol=tcp", shell=True)


def job2():

    subprocess.call("netsh interface portproxy reset", shell=True)


schedule.every().day.at("06:00").do(job1)

schedule.every().day.at("07:00").do(job2)
Probably you already visited this site: https://apscheduler.readthedocs.io/en/la.../cron.html
on the bottom you will find an example using minutes.
import subprocess
from apscheduler.schedulers.background import BackgroundScheduler
 
def job1():
 
    subprocess.call("netsh interface portproxy add v4tov4 listenaddress=192.168.0.153 listenport=1101 connectaddress=192.168.0.153 connectport=809 protocol=tcp", shell=True)
 
 
def job2():
 
    subprocess.call("netsh interface portproxy reset", shell=True)

schedule1 = BackgroundScheduler()
schedule2 = BackgroundScheduler()
schedule1.add_job(job1, 'cron', hour='06', minute='00', second='00')
schedule2.add_job(job2, 'cron', hour='07', minute='00', second='00')
schedule1.start()
schedule2.start()
there you can define seconds and days, etc. :)
Wow! I'm going to try that! Thank you so much! Been hitting my head against a wall for half a day.