Skip to content

Commit

Permalink
Add examples with ThreadPoolExecutor and ProcessPoolExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiSakuma committed May 5, 2024
1 parent 1bbe5b1 commit 043092f
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ You can try it on Jupyter Notebook online:
- [Threading](#threading)
- [Multiprocessing](#multiprocessing)
- [Multiprocessing.Pool](#multiprocessingpool)
- [ThreadPoolExecutor](#threadpoolexecutor)
- [ProcessPoolExecutor](#processpoolexecutor)
- [Features](#features)
- [A `break` and an exception](#a-break-and-an-exception)
- [Progress of starting threads and processes with progress bars](#progress-of-starting-threads-and-processes-with-progress-bars)
Expand Down Expand Up @@ -310,6 +312,64 @@ def run_with_multiprocessing_pool():
run_with_multiprocessing_pool()
```

#### ThreadPoolExecutor

An example with [`concurrent.futures.ThreadPoolExecutor`](https://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor):

```python
def task(n, name):
for _ in atpbar(range(n), name=name):
time.sleep(0.0001)


def run_with_thread_pool():

n_workers = 5
n_tasks = 10

with ThreadPoolExecutor(max_workers=n_workers) as executor:
for i in range(n_tasks):
name = 'Task {}'.format(i)
n = random.randint(5, 1000)
executor.submit(task, n, name)

flush()


run_with_thread_pool()
```

#### ProcessPoolExecutor

An example with [`concurrent.futures.ProcessPoolExecutor`](https://docs.python.org/3/library/concurrent.futures.html#processpoolexecutor):

```python
def task(n, name):
for _ in atpbar(range(n), name=name):
time.sleep(0.0001)


def run_with_process_pool():

n_workers = 5
n_tasks = 10

reporter = find_reporter()

with ProcessPoolExecutor(
max_workers=n_workers, initializer=register_reporter, initargs=(reporter,)
) as executor:
for i in range(n_tasks):
name = 'Task {}'.format(i)
n = random.randint(5, 1000)
executor.submit(task, n, name)

flush()


run_with_process_pool()
```

---

### Features
Expand Down

0 comments on commit 043092f

Please sign in to comment.