I want to have a python application (app1) running that loads a bunch of data and functionality on start-up that I will use many many times. To avoid having to restart the application and re-load all that I would like to simply call a function inside that file whenever it is needed from another python application (app2). At the same time I would like to send arguments to said function from app2 and send the results back to it. What would I need to do in both files in order to make this request-response-type of interaction work?
I apologize if my phrasing is a bit confusing.
I tried letting the app that is supposed to send requests write to a csv file which the other app scans for entries, but I would like something more direct and efficient, that only does anything if a request is sent and stays dormant until the next request comes in.
Edit: App2 is only executed to send a new request, while App1 is running permanently. So App2 needs to trigger a function inside App1 which will calculate a result based on the information loaded by App1 and the arguments sent by App2.
Edit2: Image for clarification: Image
Have a look at this: https://docs.python.org/3/library/importlib.html#importing-programmatically
I believe you're working in a decoupled modules mode, usually done with real-time applications and sometimes with GUI. I used to trigger the import and execution (in
__main__) separate script by clicking a button in a menu.Otherwise, the uglier way of doing things would be to use
subprocessand call again apython script.pyas asubprocessanytime you want.You approach with a request is good too, by brings extra effort in setting up a server to interpret the request.