|
363 | 363 | "\n",
|
364 | 364 | "> ProcessPoolExecutor (max_workers=8, on_exc=<built-in function print>,\n",
|
365 | 365 | "> pause=0, mp_context=None, initializer=None,\n",
|
366 |
| - "> initargs=())\n", |
| 366 | + "> initargs=(), max_tasks_per_child=None)\n", |
367 | 367 | "\n",
|
368 | 368 | "*Same as Python's ProcessPoolExecutor, except can pass `max_workers==0` for serial execution*"
|
369 | 369 | ],
|
|
376 | 376 | "\n",
|
377 | 377 | "> ProcessPoolExecutor (max_workers=8, on_exc=<built-in function print>,\n",
|
378 | 378 | "> pause=0, mp_context=None, initializer=None,\n",
|
379 |
| - "> initargs=())\n", |
| 379 | + "> initargs=(), max_tasks_per_child=None)\n", |
380 | 380 | "\n",
|
381 | 381 | "*Same as Python's ProcessPoolExecutor, except can pass `max_workers==0` for serial execution*"
|
382 | 382 | ]
|
|
432 | 432 | "outputs": [],
|
433 | 433 | "source": [
|
434 | 434 | "#|export\n",
|
435 |
| - "def add_one(x, a=1):\n", |
| 435 | + "def _add_one(x, a=1):\n", |
436 | 436 | " # this import is necessary for multiprocessing in notebook on windows\n",
|
437 | 437 | " import random\n",
|
438 | 438 | " time.sleep(random.random()/80)\n",
|
|
447 | 447 | "source": [
|
448 | 448 | "inp,exp = range(50),range(1,51)\n",
|
449 | 449 | "\n",
|
450 |
| - "test_eq(parallel(add_one, inp, n_workers=2), exp)\n", |
451 |
| - "test_eq(parallel(add_one, inp, threadpool=True, n_workers=2), exp)\n", |
452 |
| - "test_eq(parallel(add_one, inp, n_workers=1, a=2), range(2,52))\n", |
453 |
| - "test_eq(parallel(add_one, inp, n_workers=0), exp)\n", |
454 |
| - "test_eq(parallel(add_one, inp, n_workers=0, a=2), range(2,52))" |
| 450 | + "test_eq(parallel(_add_one, inp, n_workers=2), exp)\n", |
| 451 | + "test_eq(parallel(_add_one, inp, threadpool=True, n_workers=2), exp)\n", |
| 452 | + "test_eq(parallel(_add_one, inp, n_workers=1, a=2), range(2,52))\n", |
| 453 | + "test_eq(parallel(_add_one, inp, n_workers=0), exp)\n", |
| 454 | + "test_eq(parallel(_add_one, inp, n_workers=0, a=2), range(2,52))" |
455 | 455 | ]
|
456 | 456 | },
|
457 | 457 | {
|
|
479 | 479 | "name": "stdout",
|
480 | 480 | "output_type": "stream",
|
481 | 481 | "text": [
|
482 |
| - "0 2024-10-09 16:08:39.462154\n", |
483 |
| - "1 2024-10-09 16:08:39.715074\n", |
484 |
| - "2 2024-10-09 16:08:39.969191\n", |
485 |
| - "3 2024-10-09 16:08:40.221442\n", |
486 |
| - "4 2024-10-09 16:08:40.473224\n" |
| 482 | + "0 2024-10-12 13:30:21.217649\n", |
| 483 | + "1 2024-10-12 13:30:21.469191\n", |
| 484 | + "2 2024-10-12 13:30:21.721034\n", |
| 485 | + "3 2024-10-12 13:30:21.972793\n", |
| 486 | + "4 2024-10-12 13:30:22.223159\n" |
487 | 487 | ]
|
488 | 488 | }
|
489 | 489 | ],
|
|
520 | 520 | "parallel(die_sometimes, range(8))"
|
521 | 521 | ]
|
522 | 522 | },
|
| 523 | + { |
| 524 | + "cell_type": "code", |
| 525 | + "execution_count": null, |
| 526 | + "metadata": {}, |
| 527 | + "outputs": [], |
| 528 | + "source": [ |
| 529 | + "#|export\n", |
| 530 | + "async def parallel_async(f, items, *args, n_workers=16, total=None,\n", |
| 531 | + " timeout=None, chunksize=1, on_exc=print, **kwargs):\n", |
| 532 | + " \"Applies `f` to `items` in parallel using asyncio and a semaphore to limit concurrency.\"\n", |
| 533 | + " import asyncio\n", |
| 534 | + " if n_workers is None: n_workers = defaults.cpus\n", |
| 535 | + " semaphore = asyncio.Semaphore(n_workers)\n", |
| 536 | + " results = []\n", |
| 537 | + "\n", |
| 538 | + " async def limited_task(item):\n", |
| 539 | + " coro = f(item, *args, **kwargs) if asyncio.iscoroutinefunction(f) else asyncio.to_thread(f, item, *args, **kwargs)\n", |
| 540 | + " async with semaphore:\n", |
| 541 | + " return await asyncio.wait_for(coro, timeout) if timeout else await coro\n", |
| 542 | + "\n", |
| 543 | + " tasks = [limited_task(item) for item in items]\n", |
| 544 | + " if total is None: total = len(items)\n", |
| 545 | + " return asyncio.gather(*tasks)" |
| 546 | + ] |
| 547 | + }, |
| 548 | + { |
| 549 | + "cell_type": "code", |
| 550 | + "execution_count": null, |
| 551 | + "metadata": {}, |
| 552 | + "outputs": [], |
| 553 | + "source": [ |
| 554 | + "import asyncio" |
| 555 | + ] |
| 556 | + }, |
| 557 | + { |
| 558 | + "cell_type": "code", |
| 559 | + "execution_count": null, |
| 560 | + "metadata": {}, |
| 561 | + "outputs": [], |
| 562 | + "source": [ |
| 563 | + "async def print_time_async(i): \n", |
| 564 | + " wait = random.random()\n", |
| 565 | + " await asyncio.sleep(wait)\n", |
| 566 | + " print(i, datetime.now(), wait)\n", |
| 567 | + "\n", |
| 568 | + "await parallel_async(print_time_async, range(6), n_workers=3);" |
| 569 | + ] |
| 570 | + }, |
523 | 571 | {
|
524 | 572 | "cell_type": "code",
|
525 | 573 | "execution_count": null,
|
|
0 commit comments