Home > Enterprise >  Python: Error when running two different .py files
Python: Error when running two different .py files

Time:08-12

I'm trying to schedule two different .py files (with multi processing) to run every day at a certain intervals. I was able to successfully run the files concurrently without the schedule module

but when I try to schedule the code, I'm getting two different errors one is PermissionError: [WinError 5] Access denied and the other is the files were running sequentially (not at the same time)

here's the code that I used: This codes runs the script sequentially

from multiprocessing import Process
import time
import schedule

def one(): 
   import International
def two():
   time.sleep(2)
   import National

if __name__ == '__main__':
  Process(target=schedule.every().days.at("16:17").do(one)).start()       
  Process(target=schedule.every().days.at("16:17").do(two)).start()

while True:
    schedule.run_pending()
    time.sleep(1)

This one throws: PermissionError: [WinError 5] Access denied

from multiprocessing import Process
import time
import schedule

def both_at_once():
  def one(): 
    import International
  def two():
    import National

  if __name__ == '__main__':
    Process(target=one).start()
    Process(target=two).start()

schedule.every().days.at("16:33").do(both_at_once)

while True:
   schedule.run_pending()
   time.sleep(1)

Kindly let me know where I'm going wrong.

CodePudding user response:

This is the kind of thing you need. The __main__ code needs to file the schedule and then run the schedule. The functions it calls would then do the multiprocessing step.

from multiprocessing import Process
import time
import schedule
import International
import National

def one(): 
    Process(target=National.run).start()
def two():
    Process(target=International.run).start()

if __name__ == '__main__':
    schedule.every().days.at("16:17").do(one)
    schedule.every().days.at("16:17").do(two)
    while True:
        schedule.run_pending()
        time.sleep(1)

However, you might consider if this would be better implemented with something like cron.

  • Related