huge rework; two different task pool classes now

This commit is contained in:
2022-02-05 18:02:32 +01:00
parent f45fef6497
commit 3eae7d803f
8 changed files with 277 additions and 115 deletions

View File

@ -1,8 +1,8 @@
# Using `asyncio-taskpool`
## Simple example
## Minimal example for `SimpleTaskPool`
The minimum required setup is a "worker" coroutine function that can do something asynchronously, a main coroutine function that sets up the `TaskPool` and starts/stops the tasks as desired, eventually awaiting them all.
The minimum required setup is a "worker" coroutine function that can do something asynchronously, a main coroutine function that sets up the `SimpleTaskPool` and starts/stops the tasks as desired, eventually awaiting them all.
The following demo code enables full log output first for additional clarity. It is complete and should work as is.
@ -11,7 +11,7 @@ The following demo code enables full log output first for additional clarity. It
import logging
import asyncio
from asyncio_taskpool.pool import TaskPool
from asyncio_taskpool.pool import SimpleTaskPool
logging.getLogger().setLevel(logging.NOTSET)
@ -32,13 +32,14 @@ async def work(n: int) -> None:
async def main() -> None:
pool = TaskPool(work, (5,)) # initializes the pool; no work is being done yet
pool = SimpleTaskPool(work, (5,)) # initializes the pool; no work is being done yet
pool.start(3) # launches work tasks 0, 1, and 2
await asyncio.sleep(1.5) # lets the tasks work for a bit
pool.start() # launches work task 3
await asyncio.sleep(1.5) # lets the tasks work for a bit
pool.stop(2) # cancels tasks 3 and 2
await pool.close() # awaits all tasks, then flushes the pool
pool.close() # required for the last line
await pool.gather() # awaits all tasks, then flushes the pool
if __name__ == '__main__':
@ -46,31 +47,32 @@ if __name__ == '__main__':
```
### Output
Additional comments indicated with `<--`
```
Started work_pool_task_0
Started work_pool_task_1
Started work_pool_task_2
SimpleTaskPool-0 initialized
Started SimpleTaskPool-0_Task-0
Started SimpleTaskPool-0_Task-1
Started SimpleTaskPool-0_Task-2
did 0
did 0
did 0
Started work_pool_task_3
Started SimpleTaskPool-0_Task-3
did 1
did 1
did 1
did 0 <-- notice that the newly created task begins counting at 0
did 0
SimpleTaskPool-0 is closed!
Cancelling SimpleTaskPool-0_Task-3 ...
Cancelled SimpleTaskPool-0_Task-3
Ended SimpleTaskPool-0_Task-3
Cancelling SimpleTaskPool-0_Task-2 ...
Cancelled SimpleTaskPool-0_Task-2
Ended SimpleTaskPool-0_Task-2
did 2
did 2
did 2 <-- two taks were stopped; only tasks 0 and 1 continue "working"
Cancelling work_pool_task_2 ...
Cancelled work_pool_task_2
Exiting work_pool_task_2
Cancelling work_pool_task_3 ...
Cancelled work_pool_task_3
Exiting work_pool_task_3
did 3
did 3
Exiting work_pool_task_0
Exiting work_pool_task_1
Ended SimpleTaskPool-0_Task-0
Ended SimpleTaskPool-0_Task-1
did 4
did 4
```

View File

@ -1,7 +1,7 @@
import asyncio
import logging
from asyncio_taskpool import TaskPool, UnixControlServer
from asyncio_taskpool import SimpleTaskPool, UnixControlServer
from asyncio_taskpool.constants import PACKAGE_NAME
@ -43,7 +43,7 @@ async def main() -> None:
# We just put some integers into our queue, since all our workers actually do, is print an item and sleep for a bit.
for item in range(100):
q.put_nowait(item)
pool = TaskPool(worker, (q,)) # initializes the pool
pool = SimpleTaskPool(worker, (q,)) # initializes the pool
pool.start(3) # launches three worker tasks
control_server_task = await UnixControlServer(pool, path='/tmp/py_asyncio_taskpool.sock').serve_forever()
# We block until `.task_done()` has been called once by our workers for every item placed into the queue.
@ -53,10 +53,11 @@ async def main() -> None:
# Since our workers should now be stuck waiting for more items to pick from the queue, but no items are left,
# we can now safely cancel their tasks.
pool.stop_all()
pool.close()
# Finally we allow for all tasks to do do their cleanup, if they need to do any, upon being cancelled.
# We block until they all return or raise an exception, but since we are not interested in any of their exceptions,
# we just silently collect their exceptions along with their return values.
await pool.close(return_exceptions=True)
await pool.gather(return_exceptions=True)
await control_server_task