I have this loop I've written to find all the combinations of some set of data that I have. When iteration_depth = 3, it takes ~13 minutes to run. My laptop has 2 cores though, so I want to speed it up with multiprocessing
, but since I don't really know what I'm doing, the syntax/arguments are tripping me up.
import multiprocessing
def FindAllAffordableLineups():
all_rosters = []
roster = [None]*8
for n in range(int(iteration_depth)):
for catcher in catcher_pool[0:n]:
roster = [None]*8
roster[0] = catcher[0]
for first_baseman in first_pool[0:n]:
roster[1] = first_baseman[0]
for second_baseman in second_pool[0:n]:
roster[2] = second_baseman[0]
for third_baseman in third_pool[0:n]:
roster[3] = third_baseman[0]
for shortstop in short_pool[0:n]:
roster[4] = shortstop[0]
for outfielders in it.combinations(of_pool, 3):
roster[5:8] = outfielders[0][0], outfielders[1][0], outfielders[2][0]
salaryList = []
for player1 in roster:
for player2 in player_pool:
if player1 == player2[0]:
salaryList.append(int(player2[3]))
if sum(salaryList) <= remaining_salary:
if len(roster) == len(set(roster)):
all_rosters.append(roster[:])
if len(all_rosters) < 50:
print('Number of possible rosters found: ',len(all_rosters))
if len(all_rosters) == 50:
print("Fifty affordable rosters were found. We're not displaying every time we find another one. That would slow us down a lot.")
salaryList = []
if len(all_rosters) > 10**6:
writeRosters = open(os.path.join('Affordable Rosters.csv'), 'w', newline = '')
csvWriter = csv.writer(writeRosters)
for row in all_rosters:
csvWriter.writerow(row)
writeRosters.close()
all_rosters = []
writeRosters = open(os.path.join('Affordable Rosters.csv'), 'w', newline = '')
csvWriter = csv.writer(writeRosters)
for row in all_rosters:
csvWriter.writerow(row)
writeRosters.close()
pool = multiprocessing.Pool(processes=2)
r = pool.map(FindAllAffordableLineups())
This gives me
Traceback (most recent call last):
File "C:\Users\Owner\Desktop\Multiprocessing\11 - Find Optimal Lineup.py", line 133, in <module>
r = pool.map(FindAllAffordableLineups())
TypeError: map() missing 1 required positional argument: 'iterable'
In most of the examples I've looked at, the defined function has some argument that needs to be executed inside of the function, and that's the iterable in map.pool
command, but my function doesn't require this. How do I fix this?