0

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

FLOROID
  • 13
  • 6
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/249514/discussion-on-question-by-floroid-how-can-i-execute-a-function-in-a-running-pyth). – Dharman Nov 11 '22 at 12:46

2 Answers2

0

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 subprocess and call again a python script.py as a subprocess anytime you want.

You approach with a request is good too, by brings extra effort in setting up a server to interpret the request.

Remzinho
  • 538
  • 2
  • 9
0

Based on your comments and edits, I think the simplest way to achieve your use-case is to have one script that initially loads data, then repeatedly takes interactive input until you quit it. This is just a dummy example, of course, but hopefully it's clear enough that you can adapt the basic idea to your specific needs:

def read_data():
    # Open a file, do initial calculations, etc., and return whatever
    # data structure you use.
    return {'a': 1, 'b': 2}

def analyze(data, arg1, arg2):
    # Perform whatever calculations, based on arguments supplied. For
    # this toy example, simply add the arguments as a new key and
    # value.
    return data | {arg1: arg2}

if __name__ == '__main__':
    print('Importing data...')
    data = read_data()

    while True:
        # Loop indefinitely
        print('\nEnter value for arg1 and arg2 (separated by a space), or '
              'blank line to quit')
        response = input()
        
        # If reponse is blank, exit the loop
        if not response:
            break

        # Try splitting it into the right number of arguments. If it
        # doesn't work, restart the loop to prompt again.
        try:
            arg1, arg2 = response.split()
        except ValueError:
            print('Please enter the correct number of arguments.')
            continue

        # Finally, perform analysis and print the result.
        result = analyze(data, arg1, arg2)
        print(f'Output of analysis using arg1={arg1} and arg2={arg2}:\n'
              f'{result}')
CrazyChucky
  • 3,263
  • 4
  • 11
  • 25