Last active
March 15, 2021 04:35
-
-
Save svschannak/ecc4319faef2b17c68952a0fc567d310 to your computer and use it in GitHub Desktop.
Multiprocessing with Selenium and Python for website crawling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Pool, cpu_count | |
def run_parallel_selenium_processes(datalist, selenium_func): | |
pool = Pool() | |
# max number of parallel process | |
ITERATION_COUNT = cpu_count()-1 | |
count_per_iteration = len(datalist) / float(ITERATION_COUNT) | |
for i in range(0, ITERATION_COUNT): | |
list_start = int(count_per_iteration * i) | |
list_end = int(count_per_iteration * (i+1)) | |
pool.apply_async(selenium_func, [datalist[list_start:list_end]]) |
Thank you for the example.
I have a question: how do we get the result returned? When I apply get()
method on pool.apply_async,
the web driver starts in sequence, not simultaneously.
Could you help me with this problem, please?
Thank you for the example.
I have a question: how do we get the result returned? When I apply
get()
method onpool.apply_async,
the web driver starts in sequence, not simultaneously.Could you help me with this problem, please?
I solved this by adding this line of code in the for
loop:
results = []
results.extend(pool.apply_async(selenium_func, [datalist[list_start:list_end]]))
and by this before return
results = [results[i].get() for i in range(len(results))]
Thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First, I would like to thank you for putting this.
I tried this one and it didn't work.
Putting pool.close() and pool.join() after the for loop did the trick.