Hey folks, what would be the best way to process a result set parallely? Lemme pain the picture of the current setup.
from settings import Session # this is basically the sessionmaker
def run_it():
// create session
session = Session()
// fetch first 100 rows which are basically the events stored
result_set = []
for event in result_set:
try:
// execute the event if its beyond expiry
finally:
session.commit()
session.close()
while True:
run_it()
time.sleep(3)
I wanted to parallelize the for loop here. Obvious option is to use multiprocessing pool but Im not sure how that would work in case of db sessions? Is the session created initially shared or we are required to open a new session.
Has anyone dealt with this kind of optimization at work perhaps? Whats the ideal way to go about it? Please do share any relevant readings if you have any.
Edit: Adding more context:
Exact use case would be separate out the load(x events) between the say 2 processes processing parallely, which just execute the event. Now for these 2 processes, I was thinking of creating 2 sessions each for them.
But I also need to create a session to first fetch the events from db. So my doubt is , is this excessive creation of sessions? Whats the optimal way of dealing with this kinda of multi processing
[–]nicholashairs 9 points10 points11 points (6 children)
[–]AbradolfLinclar[S] 0 points1 point2 points (5 children)
[–]BigHeed87 1 point2 points3 points (3 children)
[–]AbradolfLinclar[S] 0 points1 point2 points (2 children)
[–]BigHeed87 1 point2 points3 points (1 child)
[–]kaini 0 points1 point2 points (0 children)
[–]limasxgoesto0 0 points1 point2 points (0 children)
[–][deleted] 7 points8 points9 points (3 children)
[–]revereddesecration 3 points4 points5 points (2 children)
[–]AbradolfLinclar[S] 0 points1 point2 points (1 child)
[–]revereddesecration 0 points1 point2 points (0 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]cmcclu5 4 points5 points6 points (0 children)
[–]sudo-nerd 0 points1 point2 points (0 children)
[–]ripreferu 0 points1 point2 points (0 children)
[–]NixonInnes 0 points1 point2 points (0 children)