0
import multiprocessing
from multiprocessing import Pool

import pandas as pd
globvar = 0
df1 = pd.DataFrame()


def set_globvar_to_one(n):
    global globvar  
    global df1
    globvar = 1
    df1 = n
    print('df1', df1)
    return df1

def print_globvar():
    print('globvar',globvar)    
    print('df1',df1)

if __name__ == "__main__":
    a ='Python Code'

    with Pool(1) as p:
        df2= p.map(set_globvar_to_one, [a])
        print ("df1----------------",df2)
    print_globvar()

Unable to access dataframes in another function using multiprocessing. Through multiprocessing I'm saving the content to dataframes df1 but when I try to access df1 in print_globvar function. I'm getting empty dataframes

0

1 Answer 1

1

By using multiprocessing you are creating a new process with its own namespace and memory (etc.). So you cannot access the memory of your new process from within your main process and vice versa. Therefore you have to pass variables via Pipe or Queue or use a Manager.

Sign up to request clarification or add additional context in comments.

1 Comment

I see example using process with queue or process with pipe. can you share an example where Pool using queue or Pool using Pipe.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.