|
63 | 63 | "outputs": [], |
64 | 64 | "source": [ |
65 | 65 | "#| export\n", |
66 | | - "def threaded(process=False):\n", |
| 66 | + "def threaded(\n", |
| 67 | + " process=False, # Create a Process instead of a Thread?\n", |
| 68 | + " daemon=False # Use daemon mode?\n", |
| 69 | + "): # The Process or Thread created, which will have `result` attr injected in once complete\n", |
67 | 70 | " \"Run `f` in a `Thread` (or `Process` if `process=True`), and returns it\"\n", |
68 | 71 | " def _r(f):\n", |
69 | 72 | " def g(_obj_td, *args, **kwargs):\n", |
|
74 | 77 | " Proc = get_context('fork').Process if sys.platform == 'darwin' else Process\n", |
75 | 78 | " res = (Thread,Proc)[process](target=g, args=args, kwargs=kwargs)\n", |
76 | 79 | " res._args = (res,)+res._args\n", |
| 80 | + " if daemon: res.daemon = True\n", |
77 | 81 | " res.start()\n", |
78 | 82 | " return res\n", |
79 | 83 | " return _f\n", |
|
146 | 150 | "a.result" |
147 | 151 | ] |
148 | 152 | }, |
| 153 | + { |
| 154 | + "cell_type": "markdown", |
| 155 | + "id": "fcd33a0d", |
| 156 | + "metadata": {}, |
| 157 | + "source": [ |
| 158 | + "Pass `daemon=True` to make the thread (or process) a daemon, so it won't prevent the parent from exiting. Useful for background services like webservers, where you don't want a still-running thread to block process shutdown." |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": null, |
| 164 | + "id": "87ba1a19", |
| 165 | + "metadata": {}, |
| 166 | + "outputs": [], |
| 167 | + "source": [ |
| 168 | + "@threaded(daemon=True)\n", |
| 169 | + "def f(): time.sleep(0.01)\n", |
| 170 | + "\n", |
| 171 | + "assert f().daemon" |
| 172 | + ] |
| 173 | + }, |
149 | 174 | { |
150 | 175 | "cell_type": "code", |
151 | 176 | "execution_count": null, |
|
169 | 194 | "name": "stdout", |
170 | 195 | "output_type": "stream", |
171 | 196 | "text": [ |
172 | | - "first\n", |
| 197 | + "first\n" |
| 198 | + ] |
| 199 | + }, |
| 200 | + { |
| 201 | + "name": "stdout", |
| 202 | + "output_type": "stream", |
| 203 | + "text": [ |
173 | 204 | "second\n" |
174 | 205 | ] |
175 | 206 | } |
|
342 | 373 | "def ThreadPoolExecutor(\n", |
343 | 374 | " max_workers:int=16, on_exc:builtin_function_or_method=print, pause:int=0, kwargs:VAR_KEYWORD\n", |
344 | 375 | "):\n", |
345 | | - "\"\"\"\n", |
346 | | - "Same as Python's ThreadPoolExecutor, except can pass `max_workers==0` for serial execution\n", |
347 | | - "\"\"\"" |
| 376 | + "\"\"\"Same as Python's ThreadPoolExecutor, except can pass `max_workers==0` for serial execution\"\"\"" |
348 | 377 | ] |
349 | 378 | }, |
350 | 379 | "execution_count": null, |
|
419 | 448 | " max_workers:int=16, on_exc:builtin_function_or_method=print, pause:int=0, mp_context:NoneType=None,\n", |
420 | 449 | " initializer:NoneType=None, initargs:tuple=(), max_tasks_per_child:NoneType=None\n", |
421 | 450 | "):\n", |
422 | | - "\"\"\"\n", |
423 | | - "Same as Python's ProcessPoolExecutor, except can pass `max_workers==0` for serial execution\n", |
424 | | - "\"\"\"" |
| 451 | + "\"\"\"Same as Python's ProcessPoolExecutor, except can pass `max_workers==0` for serial execution\"\"\"" |
425 | 452 | ] |
426 | 453 | }, |
427 | 454 | "execution_count": null, |
|
543 | 570 | "name": "stdout", |
544 | 571 | "output_type": "stream", |
545 | 572 | "text": [ |
546 | | - "2026-03-09 17:02:02.475188" |
| 573 | + "2026-05-02 06:18:54.278913" |
547 | 574 | ] |
548 | 575 | }, |
549 | 576 | { |
|
571 | 598 | "name": "stdout", |
572 | 599 | "output_type": "stream", |
573 | 600 | "text": [ |
574 | | - "2026-03-09 17:02:02.575907" |
| 601 | + "2026-05-02 06:18:54.380686" |
575 | 602 | ] |
576 | 603 | }, |
577 | 604 | { |
|
599 | 626 | "name": "stdout", |
600 | 627 | "output_type": "stream", |
601 | 628 | "text": [ |
602 | | - "2026-03-09 17:02:02.677131" |
| 629 | + "2026-05-02 06:18:54.481676" |
603 | 630 | ] |
604 | 631 | }, |
605 | 632 | { |
|
627 | 654 | "name": "stdout", |
628 | 655 | "output_type": "stream", |
629 | 656 | "text": [ |
630 | | - "2026-03-09 17:02:02.778713" |
| 657 | + "2026-05-02 06:18:54.584016" |
631 | 658 | ] |
632 | 659 | }, |
633 | 660 | { |
|
655 | 682 | "name": "stdout", |
656 | 683 | "output_type": "stream", |
657 | 684 | "text": [ |
658 | | - "2026-03-09 17:02:02.880124" |
| 685 | + "2026-05-02 06:18:54.684495" |
659 | 686 | ] |
660 | 687 | }, |
661 | 688 | { |
|
733 | 760 | "name": "stdout", |
734 | 761 | "output_type": "stream", |
735 | 762 | "text": [ |
736 | | - "1 2026-03-09 17:04:29.973713 2026-03-09 17:04:29.980911 0.006317305978820205\n", |
737 | | - "2 2026-03-09 17:04:29.973734 2026-03-09 17:04:29.985213 0.010986495373929654\n", |
738 | | - "0 2026-03-09 17:04:29.973659 2026-03-09 17:04:30.001349 0.02660853512134027\n", |
739 | | - "5 2026-03-09 17:04:30.001397 2026-03-09 17:04:30.009049 0.006747053859902045\n", |
740 | | - "3 2026-03-09 17:04:29.981235 2026-03-09 17:04:30.009845 0.028491947602613043\n", |
741 | | - "4 2026-03-09 17:04:29.985275 2026-03-09 17:04:30.012762 0.027138033640580195\n" |
| 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" |
742 | 769 | ] |
743 | 770 | } |
744 | 771 | ], |
|
770 | 797 | "name": "stdout", |
771 | 798 | "output_type": "stream", |
772 | 799 | "text": [ |
773 | | - "0 2026-03-09 17:04:31.418755 2026-03-09 17:04:31.432981 0.013117748051221312\n", |
774 | | - "1 2026-03-09 17:04:31.519680 2026-03-09 17:04:31.536720 0.016248614341927096\n" |
| 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" |
775 | 802 | ] |
776 | 803 | }, |
777 | 804 | { |
778 | 805 | "name": "stdout", |
779 | 806 | "output_type": "stream", |
780 | 807 | "text": [ |
781 | | - "2 2026-03-09 17:04:31.619834 2026-03-09 17:04:31.637578 0.01662938067120506\n", |
782 | | - "3 2026-03-09 17:04:31.719084 2026-03-09 17:04:31.747357 0.02714251612256462\n" |
| 808 | + "2" |
783 | 809 | ] |
784 | 810 | }, |
785 | 811 | { |
786 | 812 | "name": "stdout", |
787 | 813 | "output_type": "stream", |
788 | 814 | "text": [ |
789 | | - "4 2026-03-09 17:04:31.819673 2026-03-09 17:04:31.838811 0.017785103040610917\n", |
790 | | - "5 2026-03-09 17:04:31.919186 2026-03-09 17:04:31.940911 0.020535072775403555\n" |
| 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" |
| 818 | + ] |
| 819 | + }, |
| 820 | + { |
| 821 | + "name": "stdout", |
| 822 | + "output_type": "stream", |
| 823 | + "text": [ |
| 824 | + "5 2026-05-02 06:18:55.427390 2026-05-02 06:18:55.456955 0.028371390226150824\n" |
791 | 825 | ] |
792 | 826 | } |
793 | 827 | ], |
|
1050 | 1084 | ] |
1051 | 1085 | } |
1052 | 1086 | ], |
1053 | | - "metadata": {}, |
| 1087 | + "metadata": { |
| 1088 | + "solveit": { |
| 1089 | + "default_code": false, |
| 1090 | + "mode": "concise", |
| 1091 | + "use_fence": false, |
| 1092 | + "use_thinking": true, |
| 1093 | + "use_tools": true, |
| 1094 | + "ver": 2 |
| 1095 | + } |
| 1096 | + }, |
1054 | 1097 | "nbformat": 4, |
1055 | 1098 | "nbformat_minor": 5 |
1056 | 1099 | } |
0 commit comments