Scheduling like Quartz in java for python

1,114 views
Skip to first unread message

rohit dwivedi

unread,
Aug 25, 2012, 7:47:57 AM8/25/12
to apsch...@googlegroups.com


 I am trying to create a scheduler in Python like Quartz in java   ,I go through documentation but could not get the things properly
 
 My requirements are job should be persistent .

 Please suggest me some video tutorial or working example for Aps scheduler so that i could start my work


Thanks

Alex Grönholm

unread,
Aug 25, 2012, 7:49:24 AM8/25/12
to apsch...@googlegroups.com
There is no video tutorial. For working examples, see the "examples"
directory in the APScheduler tarball. If that's not enough, please
elaborate on what trouble you're having getting it to work.

rohit dwivedi

unread,
Aug 27, 2012, 4:26:06 AM8/27/12
to apsch...@googlegroups.com
Thax Alex
 

my problem )  I am trying to send emails on a specific time that will be define by the user so for wrote the scheduler program  like

def send_sms(time,Phonen,text):
    print "sending sms  on "
    data = {'Username':SMS_GATEWAY_USER, 'Pwd': SMS_GATEWAY_PASSWD,'PhoneNumber':Phonen,"PhoneMessage":text}
    url = SMS_API_BASE_URL + urllib.urlencode(data)
    handle = urllib.urlopen(url)
    handle.close()
    sys.stdout.write('Alarm! This alarm was scheduled at %s.\n' % time)


if __name__ == '__main__':
   
    getm = sys.argv[1]
    gettext = sys.argv[2]
    config = {'apscheduler.jobstores.file.class': 'apscheduler.jobstores.shelve_store:ShelveJobStore',
          'apscheduler.jobstores.file.path': '/tmp/dbfile'}
    scheduler = Scheduler()
    scheduler.add_jobstore(ShelveJobStore('getgo.db'), 'shelve')
    alarm_time = datetime.now() + timedelta(seconds=3)
    print alarm_time
    scheduler.add_date_job(send_sms, alarm_time, name='alarm',
                          jobstore='shelve', args=[datetime.now(),getm,gettext])
    scheduler.add_date_job(send_sms, alarm_time, name='alarm',
                           jobstore='shelve', args=[datetime.now(),getm,gettext])
   
    print scheduler.print_jobs()


    sys.stdout.write('To clear the alarms, delete the example.db file.\n')
    sys.stdout.write('Press Ctrl+C to exit\n')
    scheduler.start()
   
    try:
        # This is here to prevent the main thread from exiting so that the
        # scheduler has time to work -- this is rarely necessary in real world
        # applications
        time.sleep(9999)
    finally:
        # Shut down the scheduler so that the job store gets closed properly
        scheduler.shutdown()
    


ok this is what i have written now suppose if i want to send another message at different time then i will call this scheduler like sc.py  ans params 

so i run the main python file again i just want to know that if i will run it again then my previoud job will be execute or not at the TIME THAT I DEFINE FOR THAT ?

Alex Grönholm

unread,
Aug 27, 2012, 4:36:50 AM8/27/12
to apsch...@googlegroups.com
First of all, why are you configuring two ShelveJobStores (one at /tmp/dbfile and one at ./getgo.db)? You're not using the first, so why configure it?

Second, use print instead of sys.stdout.write(). Using that in the examples was a mistake on my part which I have corrected later (but not committed yet).

Finally, I'm not 100% sure what you're asking, but you should not run two instances of that script at the same time. If, however, you interrupt it and restart it, the previous jobs will still be there if they haven't executed yet.
Message has been deleted

Old Cai

unread,
Nov 6, 2012, 6:30:31 PM11/6/12
to apsch...@googlegroups.com
@Alex Grönholm: Bad news for you about "interrupt it and restart it". For me, on windows with python 2.7, interrupt not work for saving task in ShelveJobStore.
It seems that Shelf.sync() must be called for save data to persistent/disk(see docs about shelve), but it was called only once by Shelf.close() automatically in ShelveJobStore.close().
Maybe scheduler.shutdown() should be called before some program else load the job store.
I'm not sure if it is for reduce I/O cost to putting save file works at last. Maybe redis/mongodb/other database is more suitable for that large job store case.
I'm confused about how to save ShelveJobStore when it changes(task added/executed/removed/updated), if not save job store when it change, maybe makes it unsure that if a progress saved in a infinite program or not when close it or reboot the server.

Best regards.
Cai

在 2012年8月27日星期一UTC+8下午4时36分50秒,Alex Grönholm写道:

Alex Grönholm

unread,
Nov 6, 2012, 8:13:45 PM11/6/12
to apsch...@googlegroups.com
07.11.2012 01:27, 蔡逊 kirjoitti:
@Alex Grönholm Bad news for you about "interrupt it and restart it". For me, on windows with python 2.7, interrupt not work for saving task in ShelveJobStore.
It seems that Shelf.sync() must be called for save data to persistent/disk(see docs about shelve), but it was called only once by Shelf.close() automatically in ShelveJobStore.close().
Maybe scheduler.shutdown() should be called before some program else load the job store.
I'm not sure if it is for reduce I/O cost to putting save file works at last. Maybe redis/mongodb/other database is more suitable for that large job store case.
I'm confused about how to save ShelveJobStore when it changes(task added/executed/removed/updated), if not save job store when it change, maybe makes it unsure that if a progress saved in a infinite program or not when close it or reboot the server.

Yep, sorry about that...the shelve job store used to close the file after every operation but I changed that at some point and probably just forgot to add the sync() calls. I've added them back now in the unreleased 2.1.0 development version.
Best rewords.
Cai

在 2012年8月27日星期一UTC+8下午4时36分50秒,Alex Grönholm写道:
27.08.2012 11:26, rohit dwivedi kirjoitti:

蔡逊

unread,
Nov 6, 2012, 10:22:47 PM11/6/12
to apsch...@googlegroups.com
Never mind.
Your work is amazing, thanks for your contributions. It helps me a lot.

Have a nice day.


2012/11/7 Alex Grönholm <alex.g...@nextday.fi>
Reply all
Reply to author
Forward
0 new messages