asyncdefloop(): tasks = [asyncio.create_task(function()) for i inrange(10)] results = await asyncio.gather(*tasks) return results
defmain(): results = asyncio.run(loop)
joblib
1 2 3 4 5 6 7 8 9 10 11 12 13
import joblib
#目的の関数 deffunction(): pass
defloop(): tasks = [joblib.delayed(function)() for i inrange(10)] results = joblib.Parallel(n_jobs=-1)(tasks) return results
defmain(): results = loop()
concurrent.futures
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import concurrent.futures # import loky
#目的の関数 deffunction(): pass
defloop(): with concurrent.futures.ProcessPoolExecutor() as executor: # with loky.get_reusable_executor() as executor: tasks = [executor.submit(function) for i inrange(10)] results = [future.result() for future in concurrent.futures.wait(tasks).done] return results
defmain(): results = loop()
ProgressBarをつける
1
pip install tqdm
asyncio
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import asyncio # import tqdm import tqdm.asyncio
#目的の関数 asyncdeffunction(): pass
asyncdefloop(): tasks = [asyncio.create_task(function()) for i inrange(10)] # results = [await f for f in tqdm.tqdm(asyncio.as_completed(tasks), total=len(tasks))] # 古いやり方 results = await tqdm.asyncio.gather(*tasks) return results
defmain(): results = asyncio.run(loop())
joblib
1
pip install tqdm_joblib
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import joblib from tqdm_joblib import tqdm_joblib
#目的の関数 deffunction(): pass
defloop(): with tqdm_joblib(): tasks = [joblib.delayed(function)() for i inrange(10)] results = joblib.Parallel(n_jobs=-1)(tasks) return results
defmain(): results = loop()
concurrent.futures
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import concurrent.futures import tqdm
#目的の関数 deffunction(): pass
defloop(): with concurrent.futures.ProcessPoolExecutor() as executor: tasks = [executor.submit(function) for i inrange(10)] results = [future.result() for future in tqdm.tqdm(concurrent.futures.as_completed(tasks), total=len(tasks))] return results