Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fastcore/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@
'fastcore.parallel._done_pg': ('parallel.html#_done_pg', 'fastcore/parallel.py'),
'fastcore.parallel._f_pg': ('parallel.html#_f_pg', 'fastcore/parallel.py'),
'fastcore.parallel.bg_task': ('parallel.html#bg_task', 'fastcore/parallel.py'),
'fastcore.parallel.import_progress_bar': ('parallel.html#import_progress_bar', 'fastcore/parallel.py'),
'fastcore.parallel.parallel': ('parallel.html#parallel', 'fastcore/parallel.py'),
'fastcore.parallel.parallel_async': ('parallel.html#parallel_async', 'fastcore/parallel.py'),
'fastcore.parallel.parallel_gen': ('parallel.html#parallel_gen', 'fastcore/parallel.py'),
Expand Down
12 changes: 8 additions & 4 deletions fastcore/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03a_parallel.ipynb.

# %% auto #0
__all__ = ['threaded', 'startthread', 'startproc', 'parallelable', 'ThreadPoolExecutor', 'ProcessPoolExecutor', 'parallel',
'parallel_async', 'bg_task', 'run_procs', 'parallel_gen']
__all__ = ['threaded', 'startthread', 'startproc', 'parallelable', 'ThreadPoolExecutor', 'ProcessPoolExecutor',
'import_progress_bar', 'parallel', 'parallel_async', 'bg_task', 'run_procs', 'parallel_gen']

# %% ../nbs/03a_parallel.ipynb #569d18c6
from .imports import *
Expand Down Expand Up @@ -118,13 +118,16 @@ def map(self, f, items, *args, timeout=None, chunksize=1, **kwargs):
except Exception as e: self.on_exc(e)

# %% ../nbs/03a_parallel.ipynb #f5327cb3
try: from fastprogress import progress_bar
except: progress_bar = None
def import_progress_bar():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I don't think this is worth having a function for - it replaces 2 simple lines of code used in 2 places, with 4 less-simple lines of code in the function plus 2 lines to call it! :)

try: from fastprogress import progress_bar
except ImportError: return None
return progress_bar

# %% ../nbs/03a_parallel.ipynb #529e1bb1
def parallel(f, items, *args, n_workers=defaults.cpus, total=None, progress=None, pause=0,
method=None, threadpool=False, timeout=None, chunksize=1, **kwargs):
"Applies `func` in parallel to `items`, using `n_workers`"
progress_bar = import_progress_bar()
kwpool = {}
if threadpool: pool = ThreadPoolExecutor
else:
Expand Down Expand Up @@ -188,6 +191,7 @@ def _done_pg(queue, items): return (queue.get() for _ in items)
# %% ../nbs/03a_parallel.ipynb #1122caee
def parallel_gen(cls, items, n_workers=defaults.cpus, progress=True, **kwargs):
"Instantiate `cls` in `n_workers` procs & call each on a subset of `items` in parallel."
progress_bar = import_progress_bar()
if not parallelable('n_workers', n_workers): n_workers = 0
if n_workers==0:
yield from enumerate(list(cls(**kwargs)(items)))
Expand Down
18 changes: 15 additions & 3 deletions nbs/03a_parallel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,14 @@
"show_doc(ProcessPoolExecutor, title_level=4)"
]
},
{
"cell_type": "markdown",
"id": "b1d7c809",
"metadata": {},
"source": [
"We define `import_progress_bar` to lazily import, `fastprogress` depends on `fasthtml` and takes about 1 sec to load"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -468,8 +476,10 @@
"outputs": [],
"source": [
"#| export\n",
"try: from fastprogress import progress_bar\n",
"except: progress_bar = None"
"def import_progress_bar():\n",
" try: from fastprogress import progress_bar\n",
" except ImportError: return None\n",
" return progress_bar"
]
},
{
Expand All @@ -483,6 +493,7 @@
"def parallel(f, items, *args, n_workers=defaults.cpus, total=None, progress=None, pause=0,\n",
" method=None, threadpool=False, timeout=None, chunksize=1, **kwargs):\n",
" \"Applies `func` in parallel to `items`, using `n_workers`\"\n",
" progress_bar = import_progress_bar()\n",
" kwpool = {}\n",
" if threadpool: pool = ThreadPoolExecutor\n",
" else:\n",
Expand Down Expand Up @@ -980,9 +991,10 @@
"metadata": {},
"outputs": [],
"source": [
"#| export \n",
"#| export\n",
"def parallel_gen(cls, items, n_workers=defaults.cpus, progress=True, **kwargs):\n",
" \"Instantiate `cls` in `n_workers` procs & call each on a subset of `items` in parallel.\"\n",
" progress_bar = import_progress_bar()\n",
" if not parallelable('n_workers', n_workers): n_workers = 0\n",
" if n_workers==0:\n",
" yield from enumerate(list(cls(**kwargs)(items)))\n",
Expand Down
Loading