4

I am executing a FORTRAN exe from Python. The FORTRAN exe takes many minutes to complete; therefore, I need a callback to be fired when the exe finishes. The exe does not return anything back to Python, but in the callback function I will use Python to parse output text files from the FORTRAN.

For this I am using concurrent.futures and add_done_callback(), and it works. But this part of a web service and I need to have the Python method that calls subprocess.call() / Popen() to return once the FORTRAN exe is executed. Then when the FORTRAN is complete the callback function is called.

def fortran_callback(run_type, jid):

    return "Fortran finished executing"

def fortran_execute():

    from concurrent.futures import ThreadPoolExecutor as Pool

    args = "fortran.exe arg1 arg2"

    pool = Pool(max_workers=1)
    future = pool.submit(subprocess.call, args, shell=1)
    future.add_done_callback(fortran_callback(run_type, jid))
    pool.shutdown(wait=False)

    return "Fortran executed"

fortran_execute() is called when a form is submitted and I want to return "Fortran executed" without waiting for the FORTRAN to complete.

Currently the Python method returns without waiting for the FORTRAN to complete, but it also triggers the callback when it returns. The FORTRAN process continues to run, and when it eventually is completed it tries to call the callback function and an exception is thrown because the future is no longer present TypeError: 'NoneType' object is not callable.

What am I missing here to start an exe with subprocess, have the function return, and then have a callback method called only when the exe is finished executing?

4
  • I'm confused. Creating a subprocess in a new thread is not a good idea, why do you want to do that instead of using ProcessPoolExecutor?
    – laike9m
    Mar 5, 2015 at 7:30
  • I had originally used ProcessPoolExecutor, but I could not get the method that executed the ProcessPool to return after execution. From the docs this made me change to ThreadPoolExecutor: "Calling Executor or Future methods from a callable submitted to a ProcessPoolExecutor will result in deadlock." I am open to all suggestions of best practice here.
    – Flash
    Mar 5, 2015 at 14:34
  • Why? Of course you could get return value from subprocess. You access it using future.result(). Did I misunderstand your question?
    – laike9m
    Mar 5, 2015 at 14:38
  • The subprocess is a FORTRAN exe that does not return anything to Python; therefore, I did need to use future.result() (unless I misunderstand something here). I want to spawn a subprocess without Python waiting for it to complete. And when the subprocess completes to fire a callback fn.
    – Flash
    Mar 5, 2015 at 15:31

1 Answer 1

9

Ok, now I know what you want and what your problem is.

def fortran_callback(future):
    print(future.run_type, future.jid)
    return "Fortran finished executing"

def fortran_execute():

    from concurrent.futures import ProcessPoolExecutor as Pool

    args = "sleep 2; echo complete"

    pool = Pool(max_workers=1)
    future = pool.submit(subprocess.call, args, shell=1)
    future.run_type = "run_type"
    future.jid = "jid"
    future.add_done_callback(fortran_callback)

    print("Fortran executed")


if __name__ == '__main__':
    import subprocess
    fortran_execute()

Run the above code gives output:

$ python3 test.py                                                                                                                                                      
Fortran executed
complete
run_type jid
  1. Using ThreadPool is OK, but ProcessPool is better if fortran_callback is computationally expensive
  2. Callback takes only one parameter which is the future object, so what you need to do is passing parameters via future's attributes.
5
  • I came to a similar solution. Main difference is in the subprocess call. I changed shell=1/True to False (the default). With shell=True a shell would open and then run the cmd to start the FORTRAN exe. So the Futures object was associated with the shell and not the .exe, and it would complete as soon as the cmd was executed. This is why my callback was being called right after the .exe was launched. With shell=False the Futures object is linked directly to the FORTRAN exe, and the callback fires when the exe is done executing.
    – Flash
    Mar 6, 2015 at 14:35
  • 1
    Also, I needed additional params passed to my callback function. To achieve this I used functools.partial: future.add_done_callback(partial(fortran_callback, param1, param2)). When the callback is eventually fired, it already has those params passed to it, and it appends the Futures object to the list of params for the callback; therefore, my fortran_callback() function was defined like this (with futures object being the last param): def fortran_callback(param1, param2, future):
    – Flash
    Mar 6, 2015 at 14:39
  • "ProcessPool is better if fortran_callback is computationally expensive" -- but he's using subprocess, which creates a new process anyway.
    – mattalxndr
    Dec 27, 2021 at 20:33
  • @mattalxndr - the callback does not run in the subprocess-launched process. The docs say: Added callables are called in the order that they were added and are always called in a thread belonging to the process that added them. (the only 2.7 docs I could find on this say the same thing)
    – CrashNeb
    Mar 18, 2022 at 20:13
  • Interesting, thanks for looking into that.
    – mattalxndr
    Mar 20, 2022 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.