Skip to content

Commit d0c1869

Browse files
committed
use spawn on macos when not in a notebook
1 parent 67c0f3f commit d0c1869

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

fastcore/parallel.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def parallel(f, items, *args, n_workers=defaults.cpus, total=None, progress=None
106106
kwpool = {}
107107
if threadpool: pool = ThreadPoolExecutor
108108
else:
109-
if not method and sys.platform == 'darwin': method='fork'
109+
if not method and sys.platform == 'darwin' and not IN_NOTEBOOK: method='spawn'
110110
if method: kwpool['mp_context'] = get_context(method)
111111
pool = ProcessPoolExecutor
112112
with pool(n_workers, pause=pause, **kwpool) as ex:
@@ -123,21 +123,21 @@ def add_one(x, a=1):
123123
time.sleep(random.random()/80)
124124
return x+a
125125

126-
# %% ../nbs/03a_parallel.ipynb 22
126+
# %% ../nbs/03a_parallel.ipynb 21
127127
def run_procs(f, f_done, args):
128128
"Call `f` for each item in `args` in parallel, yielding `f_done`"
129129
processes = L(args).map(Process, args=arg0, target=f)
130130
for o in processes: o.start()
131131
yield from f_done()
132132
processes.map(Self.join())
133133

134-
# %% ../nbs/03a_parallel.ipynb 23
134+
# %% ../nbs/03a_parallel.ipynb 22
135135
def _f_pg(obj, queue, batch, start_idx):
136136
for i,b in enumerate(obj(batch)): queue.put((start_idx+i,b))
137137

138138
def _done_pg(queue, items): return (queue.get() for _ in items)
139139

140-
# %% ../nbs/03a_parallel.ipynb 24
140+
# %% ../nbs/03a_parallel.ipynb 23
141141
def parallel_gen(cls, items, n_workers=defaults.cpus, **kwargs):
142142
"Instantiate `cls` in `n_workers` procs & call each on a subset of `items` in parallel."
143143
if not parallelable('n_workers', n_workers): n_workers = 0

nbs/03a_parallel.ipynb

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
"Same as Python's ThreadPoolExecutor, except can pass `max_workers==0` for serial execution"
216216
],
217217
"text/plain": [
218-
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1034390a0>"
218+
"<nbdev.showdoc.BasicMarkdownRenderer>"
219219
]
220220
},
221221
"execution_count": null,
@@ -276,7 +276,7 @@
276276
"Same as Python's ProcessPoolExecutor, except can pass `max_workers==0` for serial execution"
277277
],
278278
"text/plain": [
279-
"<nbdev.showdoc.BasicMarkdownRenderer at 0x10f325e20>"
279+
"<nbdev.showdoc.BasicMarkdownRenderer>"
280280
]
281281
},
282282
"execution_count": null,
@@ -312,7 +312,7 @@
312312
" kwpool = {}\n",
313313
" if threadpool: pool = ThreadPoolExecutor\n",
314314
" else:\n",
315-
" if not method and sys.platform == 'darwin': method='fork'\n",
315+
" if not method and sys.platform == 'darwin' and not IN_NOTEBOOK: method='spawn'\n",
316316
" if method: kwpool['mp_context'] = get_context(method)\n",
317317
" pool = ProcessPoolExecutor\n",
318318
" with pool(n_workers, pause=pause, **kwpool) as ex:\n",
@@ -345,7 +345,6 @@
345345
"source": [
346346
"inp,exp = range(50),range(1,51)\n",
347347
"\n",
348-
"# test_eq(parallel(add_one, inp, method='spawn', n_workers=2, progress=False), exp)\n",
349348
"test_eq(parallel(add_one, inp, n_workers=2, progress=False), exp)\n",
350349
"test_eq(parallel(add_one, inp, threadpool=True, n_workers=2, progress=False), exp)\n",
351350
"test_eq(parallel(add_one, inp, n_workers=1, a=2), range(2,52))\n",
@@ -369,11 +368,11 @@
369368
"name": "stdout",
370369
"output_type": "stream",
371370
"text": [
372-
"0 2022-07-26 04:12:18.858918\n",
373-
"1 2022-07-26 04:12:19.110316\n",
374-
"2 2022-07-26 04:12:19.362060\n",
375-
"3 2022-07-26 04:12:19.612606\n",
376-
"4 2022-07-26 04:12:19.863821\n"
371+
"0 2022-08-07 05:10:05.999916\n",
372+
"1 2022-08-07 05:10:06.252031\n",
373+
"2 2022-08-07 05:10:06.503603\n",
374+
"3 2022-08-07 05:10:06.755216\n",
375+
"4 2022-08-07 05:10:07.006702\n"
377376
]
378377
}
379378
],
@@ -387,17 +386,6 @@
387386
"parallel(print_time, range(5), n_workers=2, pause=0.25);"
388387
]
389388
},
390-
{
391-
"cell_type": "code",
392-
"execution_count": null,
393-
"metadata": {},
394-
"outputs": [],
395-
"source": [
396-
"def die_sometimes(x):\n",
397-
"# if 3<x<6: raise Exception(f\"exc: {x}\")\n",
398-
" return x*2"
399-
]
400-
},
401389
{
402390
"cell_type": "code",
403391
"execution_count": null,
@@ -415,6 +403,11 @@
415403
}
416404
],
417405
"source": [
406+
"#|hide\n",
407+
"def die_sometimes(x):\n",
408+
"# if 3<x<6: raise Exception(f\"exc: {x}\")\n",
409+
" return x*2\n",
410+
"\n",
418411
"parallel(die_sometimes, range(8))"
419412
]
420413
},
@@ -582,7 +575,6 @@
582575
"outputs": [],
583576
"source": [
584577
"#|hide\n",
585-
"#|eval: false\n",
586578
"from nbdev import nbdev_export\n",
587579
"nbdev_export()"
588580
]

0 commit comments

Comments
 (0)