Home > Blockchain >  raise self._value NameError: name 'global_df' is not defined
raise self._value NameError: name 'global_df' is not defined

Time:04-19

when I tried to execute this code. I receive the error related to global_df is not defined. I am new to multiprocessing. The main problem is with the stock_sample where the variables such as global_df, target,global_ems, and global_ci are not defined. I am not sure how can i resolve this error. I tried to make global_df as global global_df But it didn't resolve the error.

The code is taken from enter image description here

CodePudding user response:

Stackoverflow usually likes if you provide a minimal, reproducible example. We don't want to download all your csvs to reproduce part of your code.

It should look something like this

from multiprocessing import Pool

def print_globals(text):
    print(f"{text} {global_var}")

if __name__ == "__main__":
    global_var = 1
    Pool().map(print_globals, ["hi"])

This will often also give you insight on why the issue occurs.

In your case that would be because the variables aren't in the scope of the processes you run. Instead you should pass the variables required to the functions that need it. Using global variables is considered bad practice, as you easily lose sight of where you use/change it.

A better version would be:

from multiprocessing import Pool

def print_globals(text, global_var):
    print(f"{text} {global_var}")

if __name__ == "__main__":
    global_var = 1
    Pool().starmap(print_globals, [("hi", global_var)])
  • Related