並列処理をThreadPoolExecutor()で行なっているのですが、重い処理を行おうとすると、関数が呼び出されずに、処理がスキップされます。
python
def find_link(links, que): print('find_link was called') #呼び出されません for link in links: l = link.get_attribute('href') que.put(l) with ThreadPoolExecutor() as executor: while_loop=True while(while_loop): scroll_count=scroll_count+1 scrolldown = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var scrolldown=document.body.scrollHeight;return scrolldown;") time.sleep(5) links = driver.find_elements_by_tag_name('a') que=queue.Queue() executor.submit(find_link, links, que) if scroll_count=10: while_loop=False
find_link関数の呼び出しができません。
links は取得できています。
一方下記のように、threadの作成を廃止すると実行できqueueへの追加も行えます。ただ、一つのfind_link呼び出しごとに1分ほどかかります。
python
def find_link(links, que): for link in links: l = link.get_attribute('href') que.put(l) while_loop=Truewhile(while_loop): scroll_count=scroll_count+1 scrolldown = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var scrolldown=document.body.scrollHeight;return scrolldown;") time.sleep(5) links = driver.find_elements_by_tag_name('a') que=queue.Queue() find_link(links, que) if scroll_count=10: while_loop=False
並列処理をしたいのですが、何が問題なのでしょうか?
処理の軽いものを関数にして並列処理で呼び出したところ実行することができました。
python
def find_link(links, que): #for link in links: # l = link.get_attribute('href') que.put(l)

0 コメント