You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/source/usage.rst
+13-10
Original file line number
Diff line number
Diff line change
@@ -7,18 +7,18 @@ Usage Guide
7
7
******
8
8
Design
9
9
******
10
-
The central instance within *async* is the **Pool**. A pool keeps a set of 0 or more workers which can run asynchronoously and process **Task**\ s. Tasks are added to the pool using the ``add_task`` function. Once added, the caller receives a **ChannelReader** instance which connects to a channel. Calling ``read`` on the instance will trigger the actual computation. A ChannelReader can serve as input for another task as well, which once added to the Pool, indicates a dependency between these tasks. To obtain one item from task 2, one item needs to be produced by task 1 beforehand - the pool takes care of the dependency handling as well as scheduling.
10
+
The central instance within *async* is the **Pool**. A pool keeps a set of 0 or more workers which can run asynchronoously and process **Task**\ s. Tasks are added to the pool using the ``add_task`` function. Once added, the caller receives a **ChannelReader** instance which connects to a channel. Calling ``read`` on the instance will trigger the actual computation. A ChannelReader can serve as input for another task as well, which once added to the Pool, indicates a dependency between these tasks. To obtain one item from task 2, one item needs to be produced by task 1 beforehand - the pool takes care of the dependency handling when scheduling items to be processed.
11
11
12
-
Task instances allow to define the minimum amount of items to be processed on each request, and the maximum amount of items per batch. This chunking behaviour allows you to have fine-grained control about the memory requirements as well as the actuall achieved concurrency for your chain of tasks.
12
+
Task instances allow to define the minimum amount of items to be processed on each request, and the maximum amount of items per batch. This chunking behaviour allows you to have fine-grained control about the memory requirements as well as the actually achieved concurrency for your chain of tasks.
13
13
14
-
Task chunks are the units actually being processed by the workers, the pool assures these are processed in the right order. Chunks help to bridge the gap between slowly items that take a long time to process, and those which are quickly generated. Generally, slow tasks should have small chunks, otherwise some of the workers might just end up waiting for input while slowy processing items of a big chunk take place in another worker.
14
+
Task chunks are the units actually being processed by the workers, the pool assures these are processed in the right order. Chunks help to bridge the gap between items that take a long time to process, and those which are quickly generated. Generally, slow tasks should have small chunks, otherwise some of the workers might just end up waiting for input while slowly processing items of a big chunk take place in another worker. If chunks are too big, and there are many workers, it may also be that some workers don't get any work. By default, the size of the chunk is entirely determined by the amount of items requested by the reader.
15
15
16
16
**************
17
17
The ThreadPool
18
18
**************
19
19
A thread pool is a pool implementation which uses threads as workers. ``ChannelReader``\ s are blocking channels which are used as a means of communication between tasks which are currently being processed.
20
20
21
-
The ``set_size`` method is essential, as it determines the amount of workers in the pool. It defaults to 0 for newly created pools, which is equal to a fully synchonized mode of operation - all processing is effectively done by the calling thread::
21
+
The ``set_size`` method is essential, as it determines the amount of workers in the pool. It defaults to 0 for newly created pools, which is equal to a fully synchronized mode of operation - all processing is effectively done by the calling thread::
22
22
23
23
from async.pool import ThreadPool
24
24
@@ -35,9 +35,9 @@ Currently this is the only implementation, but it was designed with the ``Multip
35
35
*****
36
36
Tasks
37
37
*****
38
-
A task encapsulates properties of a task, and how its items should be processed. The processing is usually performed per item, calling a function with one item, to receive a processed item back which will be written to into the output channel. The reader end of that channel is either held by the client of the items, or by another task which performs additional processing.
38
+
A task encapsulates properties of a task, and how its items should be processed. The processing is usually performed per item, calling a function with one item, to receive a processed item back which will be written into the output channel. The read-end of that channel is either held by the client of the items, or by another task which performs additional processing.
39
39
40
-
In the following example, a simple task is created which takes integers and multiplies them by itself::
40
+
In the following example, a simple task is created which takes integers and multiplies them by themselves::
41
41
42
42
from async.task import IteratorThreadTask
43
43
@@ -49,13 +49,16 @@ In the following example, a simple task is created which takes integers and mult
49
49
items = reader.read()
50
50
assert len(items) == 10 and items[0] == 0 and items[-1] == 81
51
51
52
+
.. note::
53
+
Due to the gil, it makes no sense to process anything using pure python - it will never run concurrently with other workers, but only asynchronously.
54
+
Concurrency can only be achieved when using c-extensions which release the GIL before long-running or blocking portions of their code.
52
55
53
56
*****************************
54
57
Channels, Readers and Writers
55
58
*****************************
56
-
Channels a the means of communication between tasks as well as clients to finally receive the processed itmes. A channel has one or more writeends and and one or more readends. Readers will block if there are less than the requested amount of items, but will wake up once the missing items where sent through the writeend.
59
+
Channels are the means of communication between tasks as well as clients to finally receive the processed items. A channel has one or more write-ends and and one or more read-ends. Readers will block if there are less than the requested amount of items, but will wake up once the missing items where sent through the write-end.
57
60
58
-
A channel's major difference over a queue is its ability to be closed, which will immediately wake up all waiting readers.
61
+
A channel's major difference to a queue is its ability to be closed, which will immediately wake up all waiting readers.
59
62
60
63
Reader Callbacks
61
64
================
@@ -64,9 +67,9 @@ The reader returned by the Pool's ``add_task`` method is a specialized version o
64
67
**************
65
68
Chaining Tasks
66
69
**************
67
-
When using different task types, chains between tasks can be created. These will be understood by the pool, which realizes the implicit task dependency and will schedule the tasks in the right order.
70
+
When using different task types, chains between tasks can be created. These will be understood by the pool, which then realizes the implicit task dependency and will schedule the tasks in the right order.
68
71
69
-
The following example creates two tasks which combine their results. As the pool only has one worker, and as the chunk size is maximized, we can be sure that the items are returned in order in this case::
72
+
The following example creates two tasks which combine their results. As the pool only has one worker, and as the chunk size is maximized, we can be sure that the items are returned in order::
0 commit comments