2

I've got a Python script that uses multiprocessing that I need Windows Task Scheduler execute at 6:45 am daily. The reason it is using multiprocessing is so that if the script stalls and has not completed after an hour, the process is terminated.

The script runs perfectly fine when run manually, but when run by Task Scheduler, it skips all code in the function I'm trying to call with multiprocessing. Pseudocode is below.

import arcpy, multiprocessing

set variables

def executeThisFunction():
    do work

if __name__ == '__main__':
    p = multiprocessing.Process(target=executeThisFunction)
    p.start()

    #Wait for 3600 seconds (1 hour) or until process finishes
    p.join(3600)

    #If thread is still active after 3600 seconds
    if p.is_alive():
        p.terminate()

It's pretty clear that when it's being run via Windows Task Scheduler, my function is being skipped, but when run via Command Prompt or by double-clicking the .py file, everything runs as intended. How do I get around this so that it runs everything in my function when being called from Windows Task Scheduler?

  • Have you tried to run a very simple script with "main"-guard but without multiprocessing by task scheduler? Does this work? – Michael Butscher Oct 20 '20 at 13:27
  • I have no idea if it will work on Windows, but an old Unix *"trick"* is to get the PPID (parent process id) and get the PID (process id) of the *"Windows Task Scheduler"* and see if they are the same - then you will know you are running under that scheduler. – Mark Setchell Oct 20 '20 at 13:40
  • So I've reviewed the script further, the script does run code within the "main"-guard but fails to call my function via p = multiprocessing.Process(target=executeThisFunction); p.start(). I'll update my question to clarify that. executeThisFunction() works fine when my script is run manually by double-clicking the .py file or via the Command Prompt. – Zachary Ordo - GISP Oct 20 '20 at 14:39
  • Do you have to do the `freeze_support()` thing in Windows? https://docs.python.org/3/library/multiprocessing.html – Mark Setchell Oct 20 '20 at 14:50
  • Maybe you didn't tell **Task Scheduler** the correct Python interpreter, whereas the Command Prompt does know the correct interpreter. – Mark Setchell Oct 21 '20 at 11:45

0 Answers0