1

I need to implement multiprocessing within a module that is called by a Flask service. I know that I need to use the protection if __name__ == "__main__" when doing multiprocessing in Python. But in that case I am not sure where the __name__ == "__main__" statement should be placed.

The first piece of my code is the Flask service:

#core_api.py
import flask
import requests
import Process

app = flask.Flask(__name__)

print("starting app")

@app.route('/query', method=['POST'])
def query():
    pl = requests.get_json()
    result = Process.process_query(pl)
    return result

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8888)

The Process.py module uses multiprocessing in a recursive way:

#Process.py
import multiprocessing
import multiprocessing.pool
from Node import nodeProcess, aggregate, processPoint

class NoDaemonProcess(multiprocessing.Process):
    def _get_daemon(self):
        return False
    def _set_daemon(self, value):
        pass
    daemon = property(_get_daemon, _set_daemon)

class MyPool(multiprocessing.pool.Pool):
    Process = NoDaemonProcess

def process_query(pl):
    if pl['nodes']:
        pool = MyPool(multiprocessing.cpu_count())
        nodeResults = [pool.apply(sub_process_query, node) for node in pl['nodes']]
        pool.close()
        res = aggregate(nodeResults)
    else:
        res = processPoint(pl)
    return res

def sub_process_query(pl):
    return process_query(pl)

This code is not running properly, and I can see it running a multitude of child process (printing endlessly "starting app"). I assume the if __name__ == '__main__' statement is needed somewhere, or a workaround, but I am not sure exactly how.

ylnor
  • 4,531
  • 2
  • 22
  • 39
  • Did you try putting line 5 to 13 inside the if condition? – Martín Nieva Apr 28 '21 at 03:13
  • Your title is very confusing and comes across not as a question but a statement/opinion. – astrochun Apr 28 '21 at 03:22
  • 1
    @astrochun I modified it, thanks – ylnor Apr 28 '21 at 03:43
  • 2
    your name is off. if \_\_name\_\_ == '\_\_main\_\_': https://stackoverflow.com/questions/419163/what-does-if-name-main-do/419185#419185 – Kenny Ostrom Apr 28 '21 at 03:45
  • 4
    `if __name__ == "main":` is not a useful thing to put *anywhere* - it's `"__main__"` that you need to be comparing to. And the creation of the `Flask()` object definitely needs to be inside that block, so it only happens once (which implies that the definition of `query()` has to be inside the block too, since its decorator requires that object). – jasonharper Apr 28 '21 at 03:45
  • @jasonharper I corrected, it was a typo and indeed ‘__main__’ in my real code. – ylnor Apr 28 '21 at 12:14
  • @MartínNieva if I do so, I won't be able to call MyPool anymore: "name 'MyPool' is not defined" – ylnor Apr 28 '21 at 17:00

0 Answers0