Skip to content

Commit 581eb79

Browse files
committed
fixes #813
1 parent b59d33a commit 581eb79

2 files changed

Lines changed: 59 additions & 45 deletions

File tree

fastcore/parallel.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ def _f(*args, **kwargs):
4747
return _r
4848

4949
# %% ../nbs/03a_parallel.ipynb #dbccb558
50-
def startthread(f):
50+
def startthread(f=None, daemon=False):
5151
"Like `threaded`, but start thread immediately"
52-
return threaded(f)()
52+
def _f(f): return threaded(daemon=daemon)(f)()
53+
return _f(f) if f else _f
54+
5355

5456
# %% ../nbs/03a_parallel.ipynb #a32d66c4
55-
def startproc(f):
57+
def startproc(f=None, daemon=False):
5658
"Like `threaded(True)`, but start Process immediately"
57-
return threaded(True)(f)()
59+
def _f(f): return threaded(True, daemon=daemon)(f)()
60+
return _f(f) if f else _f
61+
5862

5963
# %% ../nbs/03a_parallel.ipynb #44d4651b
6064
def _call(lock, pause, n, g, item):

nbs/03a_parallel.ipynb

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,10 @@
179179
"outputs": [],
180180
"source": [
181181
"#| export\n",
182-
"def startthread(f):\n",
182+
"def startthread(f=None, daemon=False):\n",
183183
" \"Like `threaded`, but start thread immediately\"\n",
184-
" return threaded(f)()"
184+
" def _f(f): return threaded(daemon=daemon)(f)()\n",
185+
" return _f(f) if f else _f\n"
185186
]
186187
},
187188
{
@@ -194,13 +195,7 @@
194195
"name": "stdout",
195196
"output_type": "stream",
196197
"text": [
197-
"first\n"
198-
]
199-
},
200-
{
201-
"name": "stdout",
202-
"output_type": "stream",
203-
"text": [
198+
"first\n",
204199
"second\n"
205200
]
206201
}
@@ -219,6 +214,19 @@
219214
"time.sleep(0.1)"
220215
]
221216
},
217+
{
218+
"cell_type": "code",
219+
"execution_count": null,
220+
"id": "c60e7d47",
221+
"metadata": {},
222+
"outputs": [],
223+
"source": [
224+
"@startthread(daemon=True)\n",
225+
"def f(): time.sleep(0.01)\n",
226+
"\n",
227+
"assert f.daemon"
228+
]
229+
},
222230
{
223231
"cell_type": "code",
224232
"execution_count": null,
@@ -227,9 +235,10 @@
227235
"outputs": [],
228236
"source": [
229237
"#| export\n",
230-
"def startproc(f):\n",
238+
"def startproc(f=None, daemon=False):\n",
231239
" \"Like `threaded(True)`, but start Process immediately\"\n",
232-
" return threaded(True)(f)()"
240+
" def _f(f): return threaded(True, daemon=daemon)(f)()\n",
241+
" return _f(f) if f else _f\n"
233242
]
234243
},
235244
{
@@ -352,9 +361,11 @@
352361
{
353362
"data": {
354363
"text/markdown": [
364+
"<div class=\"prose\">\n",
365+
"\n",
355366
"---\n",
356367
"\n",
357-
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/parallel.py#L76){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
368+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/parallel.py#L80){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
358369
"\n",
359370
"#### ThreadPoolExecutor\n",
360371
"\n",
@@ -367,7 +378,9 @@
367378
"\n",
368379
"```\n",
369380
"\n",
370-
"*Same as Python's ThreadPoolExecutor, except can pass `max_workers==0` for serial execution*"
381+
"*Same as Python's ThreadPoolExecutor, except can pass `max_workers==0` for serial execution*\n",
382+
"\n",
383+
"</div>"
371384
],
372385
"text/plain": [
373386
"def ThreadPoolExecutor(\n",
@@ -425,9 +438,11 @@
425438
{
426439
"data": {
427440
"text/markdown": [
441+
"<div class=\"prose\">\n",
442+
"\n",
428443
"---\n",
429444
"\n",
430-
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/parallel.py#L95){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
445+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/parallel.py#L99){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
431446
"\n",
432447
"#### ProcessPoolExecutor\n",
433448
"\n",
@@ -441,7 +456,9 @@
441456
"\n",
442457
"```\n",
443458
"\n",
444-
"*Same as Python's ProcessPoolExecutor, except can pass `max_workers==0` for serial execution*"
459+
"*Same as Python's ProcessPoolExecutor, except can pass `max_workers==0` for serial execution*\n",
460+
"\n",
461+
"</div>"
445462
],
446463
"text/plain": [
447464
"def ProcessPoolExecutor(\n",
@@ -570,7 +587,7 @@
570587
"name": "stdout",
571588
"output_type": "stream",
572589
"text": [
573-
"2026-05-02 06:18:54.278913"
590+
"2026-05-08 13:33:41.853055"
574591
]
575592
},
576593
{
@@ -598,7 +615,7 @@
598615
"name": "stdout",
599616
"output_type": "stream",
600617
"text": [
601-
"2026-05-02 06:18:54.380686"
618+
"2026-05-08 13:33:41.954705"
602619
]
603620
},
604621
{
@@ -626,7 +643,7 @@
626643
"name": "stdout",
627644
"output_type": "stream",
628645
"text": [
629-
"2026-05-02 06:18:54.481676"
646+
"2026-05-08 13:33:42.055784"
630647
]
631648
},
632649
{
@@ -654,7 +671,7 @@
654671
"name": "stdout",
655672
"output_type": "stream",
656673
"text": [
657-
"2026-05-02 06:18:54.584016"
674+
"2026-05-08 13:33:42.156805"
658675
]
659676
},
660677
{
@@ -682,7 +699,7 @@
682699
"name": "stdout",
683700
"output_type": "stream",
684701
"text": [
685-
"2026-05-02 06:18:54.684495"
702+
"2026-05-08 13:33:42.258407"
686703
]
687704
},
688705
{
@@ -760,12 +777,12 @@
760777
"name": "stdout",
761778
"output_type": "stream",
762779
"text": [
763-
"0 2026-05-02 06:18:54.863372 2026-05-02 06:18:54.879976 0.015635678708191197\n",
764-
"1 2026-05-02 06:18:54.863397 2026-05-02 06:18:54.882997 0.019249022668796727\n",
765-
"2 2026-05-02 06:18:54.863403 2026-05-02 06:18:54.887730 0.023758669309642773\n",
766-
"3 2026-05-02 06:18:54.880132 2026-05-02 06:18:54.900268 0.01907637860895647\n",
767-
"4 2026-05-02 06:18:54.883061 2026-05-02 06:18:54.900419 0.01732184794923949\n",
768-
"5 2026-05-02 06:18:54.887778 2026-05-02 06:18:54.903026 0.014917916227832732\n"
780+
"1 2026-05-08 13:33:42.428080 2026-05-08 13:33:42.432773 0.004103768124250407\n",
781+
"3 2026-05-08 13:33:42.432916 2026-05-08 13:33:42.434886 0.001710239998176514\n",
782+
"0 2026-05-08 13:33:42.428053 2026-05-08 13:33:42.445264 0.016137248705558976\n",
783+
"2 2026-05-08 13:33:42.428088 2026-05-08 13:33:42.454874 0.02574445812549154\n",
784+
"4 2026-05-08 13:33:42.434937 2026-05-08 13:33:42.454906 0.019200227637860365\n",
785+
"5 2026-05-08 13:33:42.445306 2026-05-08 13:33:42.477023 0.030652622230735307\n"
769786
]
770787
}
771788
],
@@ -797,31 +814,24 @@
797814
"name": "stdout",
798815
"output_type": "stream",
799816
"text": [
800-
"0 2026-05-02 06:18:54.926304 2026-05-02 06:18:54.943307 0.015931215772872948\n",
801-
"1 2026-05-02 06:18:55.027303 2026-05-02 06:18:55.033705 0.005639884521666661\n"
802-
]
803-
},
804-
{
805-
"name": "stdout",
806-
"output_type": "stream",
807-
"text": [
808-
"2"
817+
"0 2026-05-08 13:33:42.501916 2026-05-08 13:33:42.533236 0.031155971298832207\n",
818+
"1 2026-05-08 13:33:42.601971 2026-05-08 13:33:42.614399 0.0113522631875012\n"
809819
]
810820
},
811821
{
812822
"name": "stdout",
813823
"output_type": "stream",
814824
"text": [
815-
" 2026-05-02 06:18:55.127385 2026-05-02 06:18:55.144286 0.01569178427437135\n",
816-
"3 2026-05-02 06:18:55.227448 2026-05-02 06:18:55.257822 0.02922957194858668\n",
817-
"4 2026-05-02 06:18:55.327466 2026-05-02 06:18:55.337080 0.008768163173784578\n"
825+
"2 2026-05-08 13:33:42.703034 2026-05-08 13:33:42.734560 0.03126965034014501\n",
826+
"3 2026-05-08 13:33:42.803025 2026-05-08 13:33:42.810717 0.00664915846504592\n",
827+
"4 2026-05-08 13:33:42.902539 2026-05-08 13:33:42.916731 0.012804052443942782\n"
818828
]
819829
},
820830
{
821831
"name": "stdout",
822832
"output_type": "stream",
823833
"text": [
824-
"5 2026-05-02 06:18:55.427390 2026-05-02 06:18:55.456955 0.028371390226150824\n"
834+
"5 2026-05-08 13:33:43.002943 2026-05-08 13:33:43.023861 0.01970723724066066\n"
825835
]
826836
}
827837
],
@@ -925,7 +935,7 @@
925935
"output_type": "stream",
926936
"text": [
927937
"Traceback (most recent call last):\n",
928-
" File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_35266/1179310912.py\", line 2, in _fail\n",
938+
" File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_12025/1179310912.py\", line 2, in _fail\n",
929939
" async def _fail(): raise ValueError(\"this error will be printed\")\n",
930940
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
931941
"ValueError: this error will be printed\n"
@@ -1089,7 +1099,7 @@
10891099
"default_code": false,
10901100
"mode": "concise",
10911101
"use_fence": false,
1092-
"use_thinking": true,
1102+
"use_thinking": false,
10931103
"use_tools": true,
10941104
"ver": 2
10951105
}

0 commit comments

Comments
 (0)