Call Python function already running

0

I have the following scenario.

I have a function that consumes a lot of RAM and I would like to leave it running in the background for each time I call it does not have to reload all the data in memory, and when I need your result I make the call from it .

In case I need to enter a parameter and it should return a result, how to do this in Python?

**** Good as you said lets me exemplify better. *****

I have a 1.5 GB file that contains several texts and I upload it in memory, and I query in those texts. What I do not want is that with each query I have to load this file.

I want it to be running in the background while I browse the other screens I have (I'm using Django, I've created several pages and one of these pages allows me to perform queries in this file) because several screens will use this file differently, so I would like to have a process running always and when I need a return I call this process and it returns what I want

    
asked by anonymous 06.10.2018 / 00:54

1 answer

0

You are practically describing one of the advantages of using object-oriented programming.

Although in Python it is possible to have a "function" that gets a state (the data) loaded and you can, from outside it, have a run and get a value back, using generators, this is not a very common use for functions.

For objects, however, it is quite natural. Unfortunately you have not given an example of what you want to do. Of course, if the problem is time-consuming to read or produce the data, or do not have repeated copies of the data, this method helps. If only one copy of the data already gives you memory problems, you do not have much to do.

Let's assume that you need to read data from an Excel file, and then have a query that queries data that was in that worksheet: you can have a class that reads the file on the initializer (the __init__ method, and stores the data in memory, and a method that queries a given cell:

import pandas as pd

class Planilha:
    def __init__(self, caminho_do_arquivo):
         self.dados = pd.read_excel(caminho_do_arquivo)

    def consulta(self, linha, coluna):
         return self.dados.loc[linha, coluna]

And you create an instance of the worksheet class, which reads the file once, and at each data query, you can call the "query" method. (This example is very rough, since you could use the object returned by .read_excel , which is a DataFrame pandas directly, and would not even need the worksheet class - but if you need more specialized processing over the data contained in the spreadsheet, it makes sense to design such.

Anyway, everything I've described so far applies to code that runs to initialize the data - and returns the control to you, but keeps a reference to that initialized data.

If it's really how you describe this code having to "run in the background" and only respond when called (maybe with a partial result?), that's another story - you'll need to use separate threads or processes. But you can not tell without an example. It may be that "what's running" looks better if it's a worker process by adding data to a database, and you can use a normal function just to query that database. The ideal is to put in a next question the data you have, what you want, and what approaches you've already tried, with code itself.

    
06.10.2018 / 06:24