'multiprocessing in python error using concurrent.futures AttributeError : Can't get attribute 'simulate_end_season' on <module '__main__' (built-in)>
def simulate_end_season(Parameters, Team,response):
# Update result data in dicts - faster than updating a DataFrame.
points=defaultdict(int)
wins=defaultdict(int)
losses=defaultdict(int)
draws=defaultdict(int)
goals_for=defaultdict(int)
goals_against=defaultdict(int)
Max=10
games_played = 0
# Simulate all the games in a season
for match in response["matches"]:
home_team=match['homeTeam']['name']
away_team=match['awayTeam']['name']
if match['status']=='FINISHED':
home_goals=match['score']['fullTime']['homeTeam']
away_goals=match['score']['fullTime']['awayTeam']
else :
GameResult = SimulateMatch(home_team, away_team, Parameters, Team)
home_goals = GameResult[0]
away_goals =GameResult[1]
# Update the points and win/draw/loss statistics.
if home_goals > away_goals:
points[home_team] += 3
wins[home_team] += 1
losses[away_team] += 1
elif home_goals == away_goals:
points[home_team] += 1
points[away_team] += 1
draws[home_team] += 1
draws[away_team] += 1
else:
points[away_team] += 3
wins[away_team] += 1
losses[home_team] += 1
# Update the goals.
goals_for[home_team] += home_goals
goals_against[home_team] += away_goals
goals_for[away_team] += away_goals
goals_against[away_team] += home_goals
# Return the table as a DataFrame (needs to be sorted on points and goal diff).
# Build the empty table
empty_rows = np.zeros((len(Team),7), dtype=int)
season_table = pd.DataFrame(empty_rows, index=Team,columns=['points', 'wins', 'draws', 'losses', 'goals for', 'goals against', 'goal diff'])
# Fill in the table
for team in Team:
values_list = [points[team], wins[team], draws[team], losses[team], goals_for[team], goals_against[team]]
season_table.loc[team, ['points', 'wins', 'draws', 'losses', 'goals for', 'goals against']] = values_list
# Calculate the goal diff.
season_table.loc[:, 'goal diff']= season_table.loc[:, 'goals for'] - season_table.loc[:, 'goals against']
season_table=season_table.sort_values(['points', 'goal diff'], ascending=[False, False])
rank=[k for k in range(1,len(Team)+1)]
season_table.insert(1,"rank",rank,True)
season_table=season_table.sort_index()
return season_table
def simulate_n_seasons(Team, Parameters, response,n,Max = 10):
start = time.time()
team_position=np.zeros((len(Team),n),dtype=int)
GoalsFor =np.zeros((len(Team),n))
GoalsAgainst= np.zeros((len(Team),n))
Losses = np.zeros((len(Team),n))
Wins = np.zeros((len(Team),n))
Draws = np.zeros((len(Team),n))
Points = np.zeros((len(Team),n))
Name= Team.sort()
with concurrent.futures.ProcessPoolExecutor() as executor:
results= [executor.submit(simulate.simulate_end_season,Params, Team, response) for k in range(n)]
i=0
for f in concurrent.futures.as_completed(results):
season_table = f.result()
team_position[:,i]=list(season_table['rank'])
GoalsFor[:,i]=list(season_table['goals for'])
GoalsAgainst[:,i]=list(season_table['losses'])
Losses[:,i]= list(season_table['losses'])
Draws[:,i]= list(season_table['draws'])
Wins[:,i]= list(season_table['wins'])
Points[:,i]=list(season_table['points'])
i+=1
end = time.time()
Name= sorted(Team)
print(end - start)
return Name,team_position,GoalsFor,GoalsAgainst,Points,Wins,Draws,Losses
Hello everyone, I am using concurrent.futures in order to do multiproscessing for my project in python. I have read that i need to import the function used for multiprocessing from another file, i can run the function simulate_n_seasons on Google colab but on Spyder or Pycharm, i got the following error message which is in the title. What should i do ? Thanks for your help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
