1

I am using cx Oracle and schedule module in python. Following is the psuedo code.

import schedule,cx_Oracle

def db_operation(query):
    '''
    Some DB operations like
    1. Get connection
    2. Execute query
    3. commit result (in case of DML operations)
    '''


schedule.every().hour.at(":10").do(db_operation,query='some_query_1')    # Runs at 10th minute in every hour
schedule.every().day.at("13:10").do(db_operation,query='some_query_2')   # Runs at 1:10 p.m every day

Both the above scheduled jobs calls the same function (which does some DB operations) and will coincide at 13:10.

Questions:

  1. So how does the scheduler handles this scenario? Like running 2 jobs at the same time. Does it puts in some sort of queue and runs one by one even though time is same? or are they in parallel?
  2. Which one gets picked first? and if I would want the priority of first job over second, how to do it?
  3. Also, important thing is that at a time only one of these should be accessing the database, otherwise it may lead to inconsistent data. How to take care of this scenario? Like is it possible to put a sort of lock while accessing the function or should the table be locked somehow?
KL_KISNE_DEKHA_HAI
  • 649
  • 11
  • 26
  • Do you really need to do it from a client application? Can you just do it in the DB with [Oracle Scheduler](https://docs.oracle.com/en/database/oracle/oracle-database/21/admin/scheduling-jobs-with-oracle-scheduler.html#GUID-D41660D0-D88F-4D9F-8CC8-63D040EDC4E6) ? – Christopher Jones Nov 19 '22 at 09:31
  • yes i need to do from a client application only – KL_KISNE_DEKHA_HAI Nov 19 '22 at 12:09
  • you can use **Thread** , from this answer : https://stackoverflow.com/questions/2957116/how-to-run-multiple-functions-at-the-same-time – Mr Dream Feb 17 '23 at 22:41

1 Answers1

1

I took a look at the code of schedule and I have come to the following conclusions:

  1. The schedule library does not work in parallel or concurrent. Therefore, jobs that have expired are processed one after the other. They are sorted according to their due date. The job that should be performed furthest in the past is performed first.
  2. If jobs are due at the same time, schedule execute the jobs according to the FIFO scheme, regarding the creation of the jobs. So in your example, some_query_1 would be executed before some_query_2.
  3. Question three is actually self-explanatory as only one function can be executed at a time. Therefore, the functions should not actually get in each other's way.
jpotyka
  • 150
  • 1
  • 7
  • ok, so suppose that the DB insert takes 1 min for example, then control goes for query_1 first then even if query_2 has the same time, it won't get executed unless query_1 completes? – KL_KISNE_DEKHA_HAI Nov 21 '22 at 09:22
  • That is correct. You can test the behaviour by scheduling 2 functions in a test problem that use a long sleep. – jpotyka Nov 21 '22 at 12:58